]>
Commit | Line | Data |
---|---|---|
489468fe | 1 | ///////////////////////////////////////////////////////////////////////////// |
80fdcdb9 | 2 | // Name: src/osx/carbon/utilscocoa.mm |
489468fe SC |
3 | // Purpose: various cocoa mixin utility functions |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 1998-01-01 | |
a9a4f229 | 7 | // RCS-ID: $Id$ |
489468fe 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 |
33 | bool 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 | |
41 | wxMacAutoreleasePool::wxMacAutoreleasePool() | |
42 | { | |
43 | m_pool = [[NSAutoreleasePool alloc] init]; | |
44 | } | |
45 | ||
46 | wxMacAutoreleasePool::~wxMacAutoreleasePool() | |
47 | { | |
48 | [(NSAutoreleasePool*)m_pool release]; | |
49 | } | |
50 | ||
51 | #endif | |
52 | ||
b4ff8b3e | 53 | #if wxOSX_USE_COCOA |
489468fe | 54 | |
b4ff8b3e | 55 | CGContextRef wxOSXGetContextFromCurrentContext() |
489468fe SC |
56 | { |
57 | CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext] | |
58 | graphicsPort]; | |
59 | return context; | |
60 | } | |
61 | ||
03647350 | 62 | bool wxOSXLockFocus( WXWidget view) |
15fc716c | 63 | { |
f28b6f06 | 64 | return [view lockFocusIfCanDraw]; |
15fc716c SC |
65 | } |
66 | ||
03647350 | 67 | void wxOSXUnlockFocus( WXWidget view) |
15fc716c SC |
68 | { |
69 | [view unlockFocus]; | |
70 | } | |
71 | ||
489468fe SC |
72 | #endif |
73 | ||
b4ff8b3e SC |
74 | #if wxOSX_USE_IPHONE |
75 | ||
76 | CGContextRef wxOSXGetContextFromCurrentContext() | |
77 | { | |
78 | CGContextRef context = UIGraphicsGetCurrentContext(); | |
79 | return context; | |
80 | } | |
81 | ||
82 | #endif | |
83 | ||
489468fe SC |
84 | // ---------------------------------------------------------------------------- |
85 | // NSObject Utils | |
86 | // ---------------------------------------------------------------------------- | |
87 | ||
88 | void wxMacCocoaRelease( void* obj ) | |
89 | { | |
90 | [(NSObject*)obj release]; | |
91 | } | |
92 | ||
93 | void wxMacCocoaAutorelease( void* obj ) | |
94 | { | |
95 | [(NSObject*)obj autorelease]; | |
96 | } | |
97 | ||
f1c40652 | 98 | void* 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 |
109 | wxFont::wxFont(WX_NSFont nsfont) |
110 | { | |
111 | [nsfont retain]; | |
112 | wxNativeFontInfo info; | |
113 | SetNativeInfoFromNSFont(nsfont, &info); | |
114 | Create(info); | |
115 | } | |
116 | ||
03c28161 | 117 | void 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 | 152 | WX_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 | ||
d0a7d7b1 SC |
191 | static inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; } |
192 | static const NSAffineTransformStruct kSlantNSTransformStruct = { 1, 0, tan(DegToRad(11)), 1, 0, 0 }; | |
193 | ||
03c28161 | 194 | WX_NSFont wxFont::OSXCreateNSFont(const wxNativeFontInfo* info) |
f1c40652 | 195 | { |
03c28161 SC |
196 | NSFont* nsFont; |
197 | int weight = 5; | |
198 | NSFontTraitMask traits = 0; | |
199 | if (info->m_weight == wxFONTWEIGHT_BOLD) | |
3bb0d96f | 200 | { |
03c28161 SC |
201 | traits |= NSBoldFontMask; |
202 | weight = 9; | |
3bb0d96f | 203 | } |
03c28161 SC |
204 | else if (info->m_weight == wxFONTWEIGHT_LIGHT) |
205 | weight = 3; | |
03647350 | 206 | |
03c28161 SC |
207 | if (info->m_style == wxFONTSTYLE_ITALIC || info->m_style == wxFONTSTYLE_SLANT) |
208 | traits |= NSItalicFontMask; | |
6aa3f3de | 209 | |
03c28161 SC |
210 | nsFont = [[NSFontManager sharedFontManager] fontWithFamily:wxCFStringRef(info->m_faceName).AsNSString() |
211 | traits:traits weight:weight size:info->m_pointSize]; | |
d0a7d7b1 SC |
212 | |
213 | if ( nsFont == nil ) | |
214 | { | |
215 | NSFontTraitMask remainingTraits = traits; | |
216 | nsFont = [[NSFontManager sharedFontManager] fontWithFamily:wxCFStringRef(info->m_faceName).AsNSString() | |
217 | traits:0 weight:5 size:info->m_pointSize]; | |
218 | if ( nsFont == nil ) | |
219 | { | |
220 | if ( info->m_weight == wxFONTWEIGHT_BOLD ) | |
221 | { | |
222 | nsFont = [NSFont boldSystemFontOfSize:info->m_pointSize]; | |
223 | remainingTraits &= ~NSBoldFontMask; | |
224 | } | |
225 | else | |
226 | nsFont = [NSFont systemFontOfSize:info->m_pointSize]; | |
227 | } | |
228 | ||
229 | // fallback - if in doubt, let go of the bold attribute | |
230 | if ( nsFont && (remainingTraits & NSItalicFontMask) ) | |
231 | { | |
232 | NSFont* nsFontWithTraits = nil; | |
233 | if ( remainingTraits & NSBoldFontMask) | |
234 | { | |
235 | nsFontWithTraits = [[NSFontManager sharedFontManager] convertFont:nsFont toHaveTrait:NSBoldFontMask]; | |
236 | if ( nsFontWithTraits == nil ) | |
237 | { | |
238 | nsFontWithTraits = [[NSFontManager sharedFontManager] convertFont:nsFont toHaveTrait:NSItalicFontMask]; | |
239 | if ( nsFontWithTraits != nil ) | |
240 | remainingTraits &= ~NSItalicFontMask; | |
241 | } | |
242 | else | |
243 | { | |
244 | remainingTraits &= ~NSBoldFontMask; | |
245 | } | |
246 | } | |
2d64bab7 SC |
247 | // the code below causes crashes, because fontDescriptorWithMatrix is not returning a valid font descriptor |
248 | // it adds a NSCTFontMatrixAttribute as well which cannot be disposed of correctly by the autorelease pool | |
249 | // so at the moment we have to disable this and cannot synthesize italic fonts if they are not available on the system | |
250 | #if 0 | |
251 | if ( remainingTraits & NSItalicFontMask ) | |
d0a7d7b1 SC |
252 | { |
253 | if ( nsFontWithTraits == nil ) | |
254 | nsFontWithTraits = nsFont; | |
255 | ||
256 | NSAffineTransform* transform = [NSAffineTransform transform]; | |
257 | [transform setTransformStruct:kSlantNSTransformStruct]; | |
258 | [transform scaleBy:info->m_pointSize]; | |
259 | NSFontDescriptor* italicDesc = [[nsFontWithTraits fontDescriptor] fontDescriptorWithMatrix:transform]; | |
260 | if ( italicDesc != nil ) | |
261 | { | |
262 | NSFont* f = [NSFont fontWithDescriptor:italicDesc size:(CGFloat)(info->m_pointSize)]; | |
263 | if ( f != nil ) | |
264 | nsFontWithTraits = f; | |
265 | } | |
266 | } | |
2d64bab7 | 267 | #endif |
d0a7d7b1 SC |
268 | if ( nsFontWithTraits != nil ) |
269 | nsFont = nsFontWithTraits; | |
270 | } | |
271 | } | |
272 | ||
03c28161 | 273 | wxASSERT_MSG(nsFont != nil,wxT("Couldn't create nsFont")) ; |
b771d06b | 274 | wxMacCocoaRetain(nsFont); |
f1c40652 SC |
275 | return nsFont; |
276 | } | |
277 | ||
278 | #endif | |
279 | ||
b4ff8b3e SC |
280 | #if wxOSX_USE_IPHONE |
281 | ||
282 | WX_UIFont wxFont::OSXCreateUIFont(wxOSXSystemFont font, wxNativeFontInfo* info) | |
283 | { | |
284 | UIFont* uifont; | |
285 | switch( font ) | |
286 | { | |
287 | case wxOSX_SYSTEM_FONT_NORMAL: | |
288 | uifont = [UIFont systemFontOfSize:[UIFont systemFontSize]]; | |
289 | break; | |
290 | case wxOSX_SYSTEM_FONT_BOLD: | |
291 | uifont = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]]; | |
292 | break; | |
293 | case wxOSX_SYSTEM_FONT_MINI: | |
294 | case wxOSX_SYSTEM_FONT_SMALL: | |
295 | uifont = [UIFont systemFontOfSize:[UIFont smallSystemFontSize]]; | |
296 | break; | |
297 | case wxOSX_SYSTEM_FONT_MINI_BOLD: | |
298 | case wxOSX_SYSTEM_FONT_SMALL_BOLD: | |
299 | uifont = [UIFont boldSystemFontOfSize:[UIFont smallSystemFontSize]]; | |
300 | break; | |
301 | case wxOSX_SYSTEM_FONT_VIEWS: | |
302 | case wxOSX_SYSTEM_FONT_LABELS: | |
303 | uifont = [UIFont systemFontOfSize:[UIFont labelFontSize]]; | |
304 | break; | |
305 | default: | |
306 | break; | |
307 | } | |
308 | [uifont retain]; | |
309 | if ( info->m_faceName.empty()) | |
310 | { | |
311 | wxFontStyle fontstyle = wxFONTSTYLE_NORMAL; | |
312 | wxFontWeight fontweight = wxFONTWEIGHT_NORMAL; | |
313 | bool underlined = false; | |
03647350 | 314 | |
b4ff8b3e SC |
315 | int size = (int) ([uifont pointSize]+0.5); |
316 | /* | |
317 | NSFontSymbolicTraits traits = [desc symbolicTraits]; | |
03647350 | 318 | |
b4ff8b3e SC |
319 | if ( traits & NSFontBoldTrait ) |
320 | fontweight = wxFONTWEIGHT_BOLD ; | |
321 | else | |
322 | fontweight = wxFONTWEIGHT_NORMAL ; | |
323 | if ( traits & NSFontItalicTrait ) | |
324 | fontstyle = wxFONTSTYLE_ITALIC ; | |
325 | */ | |
cf4ce62c | 326 | wxCFStringRef fontname( wxCFRetain([uifont familyName]) ); |
b4ff8b3e SC |
327 | info->Init(size,wxFONTFAMILY_DEFAULT,fontstyle,fontweight,underlined, |
328 | fontname.AsString(), wxFONTENCODING_DEFAULT); | |
03647350 | 329 | |
b4ff8b3e SC |
330 | } |
331 | return uifont; | |
332 | } | |
333 | ||
334 | WX_UIFont wxFont::OSXCreateUIFont(const wxNativeFontInfo* info) | |
335 | { | |
336 | UIFont* uiFont; | |
337 | uiFont = [UIFont fontWithName:wxCFStringRef(info->m_faceName).AsNSString() size:info->m_pointSize]; | |
338 | wxMacCocoaRetain(uiFont); | |
339 | return uiFont; | |
340 | } | |
341 | ||
342 | #endif | |
489468fe SC |
343 | // ---------------------------------------------------------------------------- |
344 | // NSImage Utils | |
345 | // ---------------------------------------------------------------------------- | |
346 | ||
cf4ce62c SC |
347 | #if wxOSX_USE_IPHONE |
348 | ||
59d866ad | 349 | WX_UIImage wxOSXGetUIImageFromCGImage( CGImageRef image ) |
cf4ce62c | 350 | { |
03647350 | 351 | UIImage *newImage = [UIImage imageWithCGImage:image]; |
cf4ce62c SC |
352 | [newImage autorelease]; |
353 | return( newImage ); | |
354 | } | |
355 | ||
ca9eebc3 SC |
356 | wxBitmap wxOSXCreateSystemBitmap(const wxString& name, const wxString &client, const wxSize& size) |
357 | { | |
e5ada8e1 | 358 | #if 1 |
ca9eebc3 SC |
359 | // unfortunately this only accesses images in the app bundle, not the system wide globals |
360 | wxCFStringRef cfname(name); | |
361 | return wxBitmap( [[UIImage imageNamed:cfname.AsNSString()] CGImage] ); | |
362 | #else | |
363 | return wxBitmap(); | |
364 | #endif | |
365 | } | |
366 | ||
cf4ce62c SC |
367 | #endif |
368 | ||
f1c40652 SC |
369 | #if wxOSX_USE_COCOA |
370 | ||
e7794cf2 | 371 | wxBitmap wxOSXCreateSystemBitmap(const wxString& name, const wxString &WXUNUSED(client), const wxSize& WXUNUSED(size)) |
ca9eebc3 SC |
372 | { |
373 | wxCFStringRef cfname(name); | |
374 | wxCFRef<CGImageRef> image( wxOSXCreateCGImageFromNSImage([NSImage imageNamed:cfname.AsNSString()]) ); | |
375 | return wxBitmap( image ); | |
376 | } | |
377 | ||
489468fe | 378 | // From "Cocoa Drawing Guide:Working with Images" |
59d866ad | 379 | WX_NSImage wxOSXGetNSImageFromCGImage( CGImageRef image ) |
489468fe SC |
380 | { |
381 | NSRect imageRect = NSMakeRect(0.0, 0.0, 0.0, 0.0); | |
03647350 | 382 | |
489468fe SC |
383 | // Get the image dimensions. |
384 | imageRect.size.height = CGImageGetHeight(image); | |
385 | imageRect.size.width = CGImageGetWidth(image); | |
03647350 | 386 | |
489468fe | 387 | // Create a new image to receive the Quartz image data. |
03647350 | 388 | NSImage *newImage = [[NSImage alloc] initWithSize:imageRect.size]; |
489468fe | 389 | [newImage lockFocus]; |
03647350 | 390 | |
489468fe SC |
391 | // Get the Quartz context and draw. |
392 | CGContextRef imageContext = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort]; | |
393 | CGContextDrawImage( imageContext, *(CGRect*)&imageRect, image ); | |
394 | [newImage unlockFocus]; | |
03647350 | 395 | |
524c47aa SC |
396 | /* |
397 | // Create a bitmap rep from the image... | |
398 | NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:cgImage]; | |
399 | // Create an NSImage and add the bitmap rep to it... | |
400 | NSImage *image = [[NSImage alloc] init]; | |
401 | [image addRepresentation:bitmapRep]; | |
402 | [bitmapRep release]; | |
403 | */ | |
404 | [newImage autorelease]; | |
489468fe SC |
405 | return( newImage ); |
406 | } | |
407 | ||
267ba369 | 408 | CGImageRef wxOSXCreateCGImageFromNSImage( WX_NSImage nsimage ) |
1821877f SC |
409 | { |
410 | // based on http://www.mail-archive.com/cocoa-dev@lists.apple.com/msg18065.html | |
89a5da7c SC |
411 | |
412 | CGImageRef image = NULL; | |
413 | if (nsimage != nil) | |
414 | { | |
415 | NSSize imageSize = [nsimage size]; | |
416 | CGColorSpaceRef genericRGB = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB); | |
417 | CGContextRef context = CGBitmapContextCreate(NULL, imageSize.width, imageSize.height, 8, 0, genericRGB, kCGImageAlphaPremultipliedFirst); | |
418 | NSGraphicsContext *nsGraphicsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO]; | |
419 | [NSGraphicsContext saveGraphicsState]; | |
420 | [NSGraphicsContext setCurrentContext:nsGraphicsContext]; | |
421 | [[NSColor whiteColor] setFill]; | |
422 | NSRectFill(NSMakeRect(0.0, 0.0, imageSize.width, imageSize.height)); | |
423 | [nsimage drawAtPoint:NSZeroPoint fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0]; | |
424 | [NSGraphicsContext setCurrentContext:nsGraphicsContext]; | |
425 | image = CGBitmapContextCreateImage(context); | |
426 | CFRelease(context); | |
427 | } | |
1821877f SC |
428 | return image; |
429 | } | |
430 | ||
489468fe SC |
431 | // ---------------------------------------------------------------------------- |
432 | // NSCursor Utils | |
433 | // ---------------------------------------------------------------------------- | |
434 | ||
489468fe SC |
435 | // copied from cursor.mm |
436 | ||
437 | static NSCursor* wxGetStockCursor( short sIndex ) | |
438 | { | |
439 | ClassicCursor* pCursor = &gMacCursors[sIndex]; | |
440 | ||
441 | //Classic mac cursors are 1bps 16x16 black and white with a | |
442 | //identical mask that is 1 for on and 0 for off | |
443 | NSImage *theImage = [[NSImage alloc] initWithSize:NSMakeSize(16.0,16.0)]; | |
444 | ||
445 | //NSCursor takes an NSImage takes a number of Representations - here | |
446 | //we need only one for the raw data | |
536bfe3d SC |
447 | NSBitmapImageRep *theRep = [[NSBitmapImageRep alloc] |
448 | initWithBitmapDataPlanes: NULL // Tell Cocoa to allocate the planes for us. | |
449 | pixelsWide: 16 // All classic cursors are 16x16 | |
450 | pixelsHigh: 16 | |
451 | bitsPerSample: 1 // All classic cursors are bitmaps with bitmasks | |
452 | samplesPerPixel: 2 // Sample 0:image 1:mask | |
453 | hasAlpha: YES // Identify last sample as a mask | |
454 | isPlanar: YES // Use a separate array for each sample | |
455 | colorSpaceName: NSCalibratedWhiteColorSpace // 0.0=black 1.0=white | |
456 | bytesPerRow: 2 // Rows in each plane are on 2-byte boundaries (no pad) | |
457 | bitsPerPixel: 1]; // same as bitsPerSample since data is planar | |
458 | ||
459 | // Ensure that Cocoa allocated 2 and only 2 of the 5 possible planes | |
460 | unsigned char *planes[5]; | |
461 | [theRep getBitmapDataPlanes:planes]; | |
462 | wxASSERT(planes[0] != NULL); | |
463 | wxASSERT(planes[1] != NULL); | |
464 | wxASSERT(planes[2] == NULL); | |
465 | wxASSERT(planes[3] == NULL); | |
466 | wxASSERT(planes[4] == NULL); | |
467 | ||
468 | // NOTE1: The Cursor's bits field is white=0 black=1.. thus the bitwise-not | |
469 | // Why not use NSCalibratedBlackColorSpace? Because that reverses the | |
470 | // sense of the alpha (mask) plane. | |
471 | // NOTE2: The mask data is 0=off 1=on | |
472 | // NOTE3: Cocoa asks for "premultiplied" color planes. Since we have a | |
473 | // 1-bit color plane and a 1-bit alpha plane we can just do a bitwise-and | |
474 | // on the two. The original cursor bitmaps have 0 (white actually) for | |
475 | // any masked-off pixels. Therefore every masked-off pixel would be wrong | |
476 | // since we bit-flip all of the picture bits. In practice, Cocoa doesn't | |
477 | // seem to care, but we are following the documentation. | |
478 | ||
479 | // Fill in the color (black/white) plane | |
480 | for(int i=0; i<16; ++i) | |
489468fe | 481 | { |
536bfe3d SC |
482 | planes[0][2*i ] = (~pCursor->bits[i] & pCursor->mask[i]) >> 8 & 0xff; |
483 | planes[0][2*i+1] = (~pCursor->bits[i] & pCursor->mask[i]) & 0xff; | |
484 | } | |
485 | // Fill in the alpha (i.e. mask) plane | |
486 | for(int i=0; i<16; ++i) | |
487 | { | |
488 | planes[1][2*i ] = pCursor->mask[i] >> 8 & 0xff; | |
489 | planes[1][2*i+1] = pCursor->mask[i] & 0xff; | |
489468fe SC |
490 | } |
491 | ||
492 | //add the representation (data) to the image | |
493 | [theImage addRepresentation:theRep]; | |
494 | ||
495 | //create the new cursor | |
496 | NSCursor* theCursor = [[NSCursor alloc] initWithImage:theImage | |
497 | hotSpot:NSMakePoint(pCursor->hotspot[1], pCursor->hotspot[0]) | |
498 | ]; | |
499 | ||
500 | //do the usual cleanups | |
501 | [theRep release]; | |
502 | [theImage release]; | |
503 | ||
504 | //return the new cursor | |
505 | return theCursor; | |
506 | } | |
507 | ||
508 | WX_NSCursor wxMacCocoaCreateStockCursor( int cursor_type ) | |
509 | { | |
510 | WX_NSCursor cursor = nil; | |
511 | switch (cursor_type) | |
512 | { | |
513 | case wxCURSOR_COPY_ARROW: | |
514 | cursor = [[NSCursor arrowCursor] retain]; | |
515 | break; | |
516 | ||
517 | case wxCURSOR_WATCH: | |
518 | case wxCURSOR_WAIT: | |
4c00ce28 SC |
519 | // an arrow should be displayed by the system when things are running |
520 | // according to the HIG | |
521 | // cursor = [[NSCursor arrowCursor] retain]; | |
522 | // but for crossplatform compatibility we display a watch cursor | |
523 | cursor = wxGetStockCursor(kwxCursorWatch); | |
489468fe SC |
524 | break; |
525 | ||
526 | case wxCURSOR_IBEAM: | |
527 | cursor = [[NSCursor IBeamCursor] retain]; | |
528 | break; | |
529 | ||
530 | case wxCURSOR_CROSS: | |
531 | cursor = [[NSCursor crosshairCursor] retain]; | |
532 | break; | |
533 | ||
534 | case wxCURSOR_SIZENWSE: | |
535 | cursor = wxGetStockCursor(kwxCursorSizeNWSE); | |
536 | break; | |
537 | ||
538 | case wxCURSOR_SIZENESW: | |
539 | cursor = wxGetStockCursor(kwxCursorSizeNESW); | |
540 | break; | |
541 | ||
542 | case wxCURSOR_SIZEWE: | |
543 | cursor = [[NSCursor resizeLeftRightCursor] retain]; | |
544 | break; | |
545 | ||
546 | case wxCURSOR_SIZENS: | |
547 | cursor = [[NSCursor resizeUpDownCursor] retain]; | |
548 | break; | |
549 | ||
550 | case wxCURSOR_SIZING: | |
551 | cursor = wxGetStockCursor(kwxCursorSize); | |
552 | break; | |
553 | ||
554 | case wxCURSOR_HAND: | |
555 | cursor = [[NSCursor pointingHandCursor] retain]; | |
556 | break; | |
03647350 | 557 | |
489468fe SC |
558 | case wxCURSOR_BULLSEYE: |
559 | cursor = wxGetStockCursor(kwxCursorBullseye); | |
560 | break; | |
561 | ||
562 | case wxCURSOR_PENCIL: | |
563 | cursor = wxGetStockCursor(kwxCursorPencil); | |
564 | break; | |
565 | ||
566 | case wxCURSOR_MAGNIFIER: | |
567 | cursor = wxGetStockCursor(kwxCursorMagnifier); | |
568 | break; | |
569 | ||
570 | case wxCURSOR_NO_ENTRY: | |
571 | cursor = wxGetStockCursor(kwxCursorNoEntry); | |
572 | break; | |
573 | ||
574 | case wxCURSOR_PAINT_BRUSH: | |
575 | cursor = wxGetStockCursor(kwxCursorPaintBrush); | |
576 | break; | |
577 | ||
578 | case wxCURSOR_POINT_LEFT: | |
579 | cursor = wxGetStockCursor(kwxCursorPointLeft); | |
580 | break; | |
581 | ||
582 | case wxCURSOR_POINT_RIGHT: | |
583 | cursor = wxGetStockCursor(kwxCursorPointRight); | |
584 | break; | |
585 | ||
586 | case wxCURSOR_QUESTION_ARROW: | |
587 | cursor = wxGetStockCursor(kwxCursorQuestionArrow); | |
588 | break; | |
589 | ||
590 | case wxCURSOR_BLANK: | |
591 | cursor = wxGetStockCursor(kwxCursorBlank); | |
592 | break; | |
593 | ||
594 | case wxCURSOR_RIGHT_ARROW: | |
595 | cursor = wxGetStockCursor(kwxCursorRightArrow); | |
596 | break; | |
597 | ||
598 | case wxCURSOR_SPRAYCAN: | |
599 | cursor = wxGetStockCursor(kwxCursorRoller); | |
600 | break; | |
601 | ||
179c2d91 SC |
602 | case wxCURSOR_OPEN_HAND: |
603 | cursor = [[NSCursor openHandCursor] retain]; | |
604 | break; | |
605 | ||
606 | case wxCURSOR_CLOSED_HAND: | |
607 | cursor = [[NSCursor closedHandCursor] retain]; | |
608 | break; | |
609 | ||
489468fe SC |
610 | case wxCURSOR_CHAR: |
611 | case wxCURSOR_ARROW: | |
612 | case wxCURSOR_LEFT_BUTTON: | |
613 | case wxCURSOR_RIGHT_BUTTON: | |
614 | case wxCURSOR_MIDDLE_BUTTON: | |
615 | default: | |
616 | cursor = [[NSCursor arrowCursor] retain]; | |
617 | break; | |
618 | } | |
619 | return cursor; | |
620 | } | |
621 | ||
622 | // C-based style wrapper routines around NSCursor | |
623 | WX_NSCursor wxMacCocoaCreateCursorFromCGImage( CGImageRef cgImageRef, float hotSpotX, float hotSpotY ) | |
624 | { | |
625 | static BOOL firstTime = YES; | |
03647350 | 626 | |
489468fe SC |
627 | if ( firstTime ) |
628 | { | |
629 | // 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 | |
630 | [[[NSWindow alloc] init] release]; | |
631 | firstTime = NO; | |
632 | } | |
03647350 | 633 | |
59d866ad | 634 | NSImage *nsImage = wxOSXGetNSImageFromCGImage( cgImageRef ); |
489468fe | 635 | NSCursor *cursor = [[NSCursor alloc] initWithImage:nsImage hotSpot:NSMakePoint( hotSpotX, hotSpotY )]; |
03647350 | 636 | |
489468fe SC |
637 | return cursor; |
638 | } | |
639 | ||
640 | void wxMacCocoaSetCursor( WX_NSCursor cursor ) | |
641 | { | |
642 | [cursor set]; | |
643 | } | |
644 | ||
645 | void wxMacCocoaHideCursor() | |
646 | { | |
647 | [NSCursor hide]; | |
648 | } | |
649 | ||
650 | void wxMacCocoaShowCursor() | |
651 | { | |
652 | [NSCursor unhide]; | |
653 | } | |
654 | ||
655 | #endif | |
b2680ced | 656 |