]>
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 | ||
d7872f62 SC |
14 | #ifndef WX_PRECOMP |
15 | #include "wx/object.h" | |
16 | #endif | |
17 | ||
b2680ced | 18 | #if wxOSX_USE_COCOA_OR_CARBON |
489468fe | 19 | #include <Cocoa/Cocoa.h> |
b2680ced SC |
20 | #else |
21 | #import <UIKit/UIKit.h> | |
22 | #endif | |
489468fe SC |
23 | |
24 | #ifdef __WXMAC__ | |
1f0c8f31 | 25 | #include "wx/osx/private.h" |
489468fe SC |
26 | #endif |
27 | ||
f1c40652 SC |
28 | #include "wx/fontutil.h" |
29 | ||
03c28161 SC |
30 | #if wxOSX_USE_COCOA |
31 | #include "wx/cocoa/string.h" | |
32 | #endif | |
33 | ||
489468fe SC |
34 | #ifdef __WXMAC__ |
35 | ||
b2680ced | 36 | #if wxOSX_USE_CARBON |
489468fe SC |
37 | bool wxMacInitCocoa() |
38 | { | |
39 | bool cocoaLoaded = NSApplicationLoad(); | |
40 | wxASSERT_MSG(cocoaLoaded,wxT("Couldn't load Cocoa in Carbon Environment")) ; | |
41 | return cocoaLoaded; | |
42 | } | |
b2680ced | 43 | #endif |
489468fe SC |
44 | |
45 | wxMacAutoreleasePool::wxMacAutoreleasePool() | |
46 | { | |
47 | m_pool = [[NSAutoreleasePool alloc] init]; | |
48 | } | |
49 | ||
50 | wxMacAutoreleasePool::~wxMacAutoreleasePool() | |
51 | { | |
52 | [(NSAutoreleasePool*)m_pool release]; | |
53 | } | |
54 | ||
55 | #endif | |
56 | ||
b4ff8b3e | 57 | #if wxOSX_USE_COCOA |
489468fe | 58 | |
b4ff8b3e | 59 | CGContextRef wxOSXGetContextFromCurrentContext() |
489468fe SC |
60 | { |
61 | CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext] | |
62 | graphicsPort]; | |
63 | return context; | |
64 | } | |
65 | ||
03647350 | 66 | bool wxOSXLockFocus( WXWidget view) |
15fc716c | 67 | { |
f28b6f06 | 68 | return [view lockFocusIfCanDraw]; |
15fc716c SC |
69 | } |
70 | ||
03647350 | 71 | void wxOSXUnlockFocus( WXWidget view) |
15fc716c SC |
72 | { |
73 | [view unlockFocus]; | |
74 | } | |
75 | ||
489468fe SC |
76 | #endif |
77 | ||
b4ff8b3e SC |
78 | #if wxOSX_USE_IPHONE |
79 | ||
80 | CGContextRef wxOSXGetContextFromCurrentContext() | |
81 | { | |
82 | CGContextRef context = UIGraphicsGetCurrentContext(); | |
83 | return context; | |
84 | } | |
85 | ||
86 | #endif | |
87 | ||
489468fe SC |
88 | // ---------------------------------------------------------------------------- |
89 | // NSObject Utils | |
90 | // ---------------------------------------------------------------------------- | |
91 | ||
92 | void wxMacCocoaRelease( void* obj ) | |
93 | { | |
94 | [(NSObject*)obj release]; | |
95 | } | |
96 | ||
97 | void wxMacCocoaAutorelease( void* obj ) | |
98 | { | |
99 | [(NSObject*)obj autorelease]; | |
100 | } | |
101 | ||
f1c40652 | 102 | void* wxMacCocoaRetain( void* obj ) |
489468fe SC |
103 | { |
104 | [(NSObject*)obj retain]; | |
f1c40652 | 105 | return obj; |
489468fe SC |
106 | } |
107 | ||
f1c40652 SC |
108 | // ---------------------------------------------------------------------------- |
109 | // NSFont Utils | |
110 | // ---------------------------------------------------------------------------- | |
111 | ||
b2680ced | 112 | #if wxOSX_USE_COCOA |
f7946add KO |
113 | wxFont::wxFont(WX_NSFont nsfont) |
114 | { | |
115 | [nsfont retain]; | |
116 | wxNativeFontInfo info; | |
117 | SetNativeInfoFromNSFont(nsfont, &info); | |
118 | Create(info); | |
119 | } | |
120 | ||
03c28161 | 121 | void wxFont::SetNativeInfoFromNSFont(WX_NSFont theFont, wxNativeFontInfo* info) |
f7946add | 122 | { |
f7946add KO |
123 | if ( info->m_faceName.empty()) |
124 | { | |
03c28161 SC |
125 | //Get more information about the user's chosen font |
126 | NSFontTraitMask theTraits = [[NSFontManager sharedFontManager] traitsOfFont:theFont]; | |
127 | int theFontWeight = [[NSFontManager sharedFontManager] weightOfFont:theFont]; | |
128 | ||
129 | wxFontFamily fontFamily = wxFONTFAMILY_DEFAULT; | |
130 | //Set the wx font to the appropriate data | |
131 | if(theTraits & NSFixedPitchFontMask) | |
132 | fontFamily = wxFONTFAMILY_TELETYPE; | |
133 | ||
f7946add KO |
134 | wxFontStyle fontstyle = wxFONTSTYLE_NORMAL; |
135 | wxFontWeight fontweight = wxFONTWEIGHT_NORMAL; | |
136 | bool underlined = false; | |
137 | ||
03c28161 SC |
138 | int size = (int) ([theFont pointSize]+0.5); |
139 | ||
140 | if ( theFontWeight >= 9 ) | |
f7946add | 141 | fontweight = wxFONTWEIGHT_BOLD ; |
03c28161 SC |
142 | else if ( theFontWeight < 5 ) |
143 | fontweight = wxFONTWEIGHT_LIGHT; | |
f7946add KO |
144 | else |
145 | fontweight = wxFONTWEIGHT_NORMAL ; | |
03c28161 SC |
146 | |
147 | if ( theTraits & NSItalicFontMask ) | |
f7946add KO |
148 | fontstyle = wxFONTSTYLE_ITALIC ; |
149 | ||
03c28161 SC |
150 | info->Init(size,fontFamily,fontstyle,fontweight,underlined, |
151 | wxStringWithNSString([theFont familyName]), wxFONTENCODING_DEFAULT); | |
f7946add KO |
152 | |
153 | } | |
f7946add | 154 | } |
b2680ced | 155 | |
aa6208d9 | 156 | WX_NSFont wxFont::OSXCreateNSFont(wxOSXSystemFont font, wxNativeFontInfo* info) |
f1c40652 | 157 | { |
de0d2095 | 158 | NSFont* nsfont = nil; |
f1c40652 SC |
159 | switch( font ) |
160 | { | |
161 | case wxOSX_SYSTEM_FONT_NORMAL: | |
162 | nsfont = [NSFont systemFontOfSize:[NSFont systemFontSize]]; | |
163 | break; | |
164 | case wxOSX_SYSTEM_FONT_BOLD: | |
165 | nsfont = [NSFont boldSystemFontOfSize:[NSFont systemFontSize]]; | |
166 | break; | |
167 | case wxOSX_SYSTEM_FONT_SMALL: | |
168 | nsfont = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]]; | |
169 | break; | |
170 | case wxOSX_SYSTEM_FONT_SMALL_BOLD: | |
171 | nsfont = [NSFont boldSystemFontOfSize:[NSFont smallSystemFontSize]]; | |
172 | break; | |
173 | case wxOSX_SYSTEM_FONT_MINI: | |
b4ff8b3e SC |
174 | nsfont = [NSFont systemFontOfSize: |
175 | [NSFont systemFontSizeForControlSize:NSMiniControlSize]]; | |
f1c40652 SC |
176 | break; |
177 | case wxOSX_SYSTEM_FONT_MINI_BOLD: | |
178 | nsfont = [NSFont boldSystemFontOfSize: | |
179 | [NSFont systemFontSizeForControlSize:NSMiniControlSize]]; | |
180 | break; | |
181 | case wxOSX_SYSTEM_FONT_LABELS: | |
b4ff8b3e | 182 | nsfont = [NSFont labelFontOfSize:[NSFont labelFontSize]]; |
f1c40652 SC |
183 | break; |
184 | case wxOSX_SYSTEM_FONT_VIEWS: | |
185 | nsfont = [NSFont controlContentFontOfSize:0]; | |
186 | break; | |
187 | default: | |
188 | break; | |
189 | } | |
190 | [nsfont retain]; | |
f7946add | 191 | SetNativeInfoFromNSFont(nsfont, info); |
f1c40652 SC |
192 | return nsfont; |
193 | } | |
194 | ||
d0a7d7b1 | 195 | static inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; } |
f0d3d77b | 196 | static const NSAffineTransformStruct kSlantNSTransformStruct = { 1, 0, static_cast<CGFloat>(tan(DegToRad(11))), 1, 0, 0 }; |
d0a7d7b1 | 197 | |
03c28161 | 198 | WX_NSFont wxFont::OSXCreateNSFont(const wxNativeFontInfo* info) |
f1c40652 | 199 | { |
03c28161 SC |
200 | NSFont* nsFont; |
201 | int weight = 5; | |
202 | NSFontTraitMask traits = 0; | |
203 | if (info->m_weight == wxFONTWEIGHT_BOLD) | |
3bb0d96f | 204 | { |
03c28161 SC |
205 | traits |= NSBoldFontMask; |
206 | weight = 9; | |
3bb0d96f | 207 | } |
03c28161 SC |
208 | else if (info->m_weight == wxFONTWEIGHT_LIGHT) |
209 | weight = 3; | |
03647350 | 210 | |
03c28161 SC |
211 | if (info->m_style == wxFONTSTYLE_ITALIC || info->m_style == wxFONTSTYLE_SLANT) |
212 | traits |= NSItalicFontMask; | |
6aa3f3de | 213 | |
03c28161 SC |
214 | nsFont = [[NSFontManager sharedFontManager] fontWithFamily:wxCFStringRef(info->m_faceName).AsNSString() |
215 | traits:traits weight:weight size:info->m_pointSize]; | |
d0a7d7b1 SC |
216 | |
217 | if ( nsFont == nil ) | |
218 | { | |
219 | NSFontTraitMask remainingTraits = traits; | |
220 | nsFont = [[NSFontManager sharedFontManager] fontWithFamily:wxCFStringRef(info->m_faceName).AsNSString() | |
221 | traits:0 weight:5 size:info->m_pointSize]; | |
222 | if ( nsFont == nil ) | |
223 | { | |
224 | if ( info->m_weight == wxFONTWEIGHT_BOLD ) | |
225 | { | |
226 | nsFont = [NSFont boldSystemFontOfSize:info->m_pointSize]; | |
227 | remainingTraits &= ~NSBoldFontMask; | |
228 | } | |
229 | else | |
230 | nsFont = [NSFont systemFontOfSize:info->m_pointSize]; | |
231 | } | |
232 | ||
233 | // fallback - if in doubt, let go of the bold attribute | |
234 | if ( nsFont && (remainingTraits & NSItalicFontMask) ) | |
235 | { | |
236 | NSFont* nsFontWithTraits = nil; | |
237 | if ( remainingTraits & NSBoldFontMask) | |
238 | { | |
239 | nsFontWithTraits = [[NSFontManager sharedFontManager] convertFont:nsFont toHaveTrait:NSBoldFontMask]; | |
240 | if ( nsFontWithTraits == nil ) | |
241 | { | |
242 | nsFontWithTraits = [[NSFontManager sharedFontManager] convertFont:nsFont toHaveTrait:NSItalicFontMask]; | |
243 | if ( nsFontWithTraits != nil ) | |
244 | remainingTraits &= ~NSItalicFontMask; | |
245 | } | |
246 | else | |
247 | { | |
248 | remainingTraits &= ~NSBoldFontMask; | |
249 | } | |
250 | } | |
2d64bab7 SC |
251 | // the code below causes crashes, because fontDescriptorWithMatrix is not returning a valid font descriptor |
252 | // it adds a NSCTFontMatrixAttribute as well which cannot be disposed of correctly by the autorelease pool | |
253 | // so at the moment we have to disable this and cannot synthesize italic fonts if they are not available on the system | |
254 | #if 0 | |
255 | if ( remainingTraits & NSItalicFontMask ) | |
d0a7d7b1 SC |
256 | { |
257 | if ( nsFontWithTraits == nil ) | |
258 | nsFontWithTraits = nsFont; | |
259 | ||
260 | NSAffineTransform* transform = [NSAffineTransform transform]; | |
261 | [transform setTransformStruct:kSlantNSTransformStruct]; | |
262 | [transform scaleBy:info->m_pointSize]; | |
263 | NSFontDescriptor* italicDesc = [[nsFontWithTraits fontDescriptor] fontDescriptorWithMatrix:transform]; | |
264 | if ( italicDesc != nil ) | |
265 | { | |
266 | NSFont* f = [NSFont fontWithDescriptor:italicDesc size:(CGFloat)(info->m_pointSize)]; | |
267 | if ( f != nil ) | |
268 | nsFontWithTraits = f; | |
269 | } | |
270 | } | |
2d64bab7 | 271 | #endif |
d0a7d7b1 SC |
272 | if ( nsFontWithTraits != nil ) |
273 | nsFont = nsFontWithTraits; | |
274 | } | |
275 | } | |
276 | ||
03c28161 | 277 | wxASSERT_MSG(nsFont != nil,wxT("Couldn't create nsFont")) ; |
b771d06b | 278 | wxMacCocoaRetain(nsFont); |
f1c40652 SC |
279 | return nsFont; |
280 | } | |
281 | ||
282 | #endif | |
283 | ||
b4ff8b3e SC |
284 | #if wxOSX_USE_IPHONE |
285 | ||
286 | WX_UIFont wxFont::OSXCreateUIFont(wxOSXSystemFont font, wxNativeFontInfo* info) | |
287 | { | |
288 | UIFont* uifont; | |
289 | switch( font ) | |
290 | { | |
291 | case wxOSX_SYSTEM_FONT_NORMAL: | |
292 | uifont = [UIFont systemFontOfSize:[UIFont systemFontSize]]; | |
293 | break; | |
294 | case wxOSX_SYSTEM_FONT_BOLD: | |
295 | uifont = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]]; | |
296 | break; | |
297 | case wxOSX_SYSTEM_FONT_MINI: | |
298 | case wxOSX_SYSTEM_FONT_SMALL: | |
299 | uifont = [UIFont systemFontOfSize:[UIFont smallSystemFontSize]]; | |
300 | break; | |
301 | case wxOSX_SYSTEM_FONT_MINI_BOLD: | |
302 | case wxOSX_SYSTEM_FONT_SMALL_BOLD: | |
303 | uifont = [UIFont boldSystemFontOfSize:[UIFont smallSystemFontSize]]; | |
304 | break; | |
305 | case wxOSX_SYSTEM_FONT_VIEWS: | |
306 | case wxOSX_SYSTEM_FONT_LABELS: | |
307 | uifont = [UIFont systemFontOfSize:[UIFont labelFontSize]]; | |
308 | break; | |
309 | default: | |
310 | break; | |
311 | } | |
312 | [uifont retain]; | |
313 | if ( info->m_faceName.empty()) | |
314 | { | |
315 | wxFontStyle fontstyle = wxFONTSTYLE_NORMAL; | |
316 | wxFontWeight fontweight = wxFONTWEIGHT_NORMAL; | |
317 | bool underlined = false; | |
03647350 | 318 | |
b4ff8b3e SC |
319 | int size = (int) ([uifont pointSize]+0.5); |
320 | /* | |
321 | NSFontSymbolicTraits traits = [desc symbolicTraits]; | |
03647350 | 322 | |
b4ff8b3e SC |
323 | if ( traits & NSFontBoldTrait ) |
324 | fontweight = wxFONTWEIGHT_BOLD ; | |
325 | else | |
326 | fontweight = wxFONTWEIGHT_NORMAL ; | |
327 | if ( traits & NSFontItalicTrait ) | |
328 | fontstyle = wxFONTSTYLE_ITALIC ; | |
329 | */ | |
cf4ce62c | 330 | wxCFStringRef fontname( wxCFRetain([uifont familyName]) ); |
b4ff8b3e SC |
331 | info->Init(size,wxFONTFAMILY_DEFAULT,fontstyle,fontweight,underlined, |
332 | fontname.AsString(), wxFONTENCODING_DEFAULT); | |
03647350 | 333 | |
b4ff8b3e SC |
334 | } |
335 | return uifont; | |
336 | } | |
337 | ||
338 | WX_UIFont wxFont::OSXCreateUIFont(const wxNativeFontInfo* info) | |
339 | { | |
340 | UIFont* uiFont; | |
341 | uiFont = [UIFont fontWithName:wxCFStringRef(info->m_faceName).AsNSString() size:info->m_pointSize]; | |
342 | wxMacCocoaRetain(uiFont); | |
343 | return uiFont; | |
344 | } | |
345 | ||
78f21217 SC |
346 | #endif |
347 | ||
348 | // ---------------------------------------------------------------------------- | |
349 | // NSWindow Utils | |
350 | // ---------------------------------------------------------------------------- | |
351 | ||
352 | #if wxOSX_USE_COCOA | |
353 | ||
354 | WXWindow wxOSXGetMainWindow() | |
355 | { | |
356 | return [NSApp mainWindow]; | |
357 | } | |
358 | ||
b4ff8b3e | 359 | #endif |
489468fe SC |
360 | // ---------------------------------------------------------------------------- |
361 | // NSImage Utils | |
362 | // ---------------------------------------------------------------------------- | |
363 | ||
cf4ce62c SC |
364 | #if wxOSX_USE_IPHONE |
365 | ||
59d866ad | 366 | WX_UIImage wxOSXGetUIImageFromCGImage( CGImageRef image ) |
cf4ce62c | 367 | { |
03647350 | 368 | UIImage *newImage = [UIImage imageWithCGImage:image]; |
cf4ce62c SC |
369 | [newImage autorelease]; |
370 | return( newImage ); | |
371 | } | |
372 | ||
ca9eebc3 SC |
373 | wxBitmap wxOSXCreateSystemBitmap(const wxString& name, const wxString &client, const wxSize& size) |
374 | { | |
e5ada8e1 | 375 | #if 1 |
ca9eebc3 SC |
376 | // unfortunately this only accesses images in the app bundle, not the system wide globals |
377 | wxCFStringRef cfname(name); | |
378 | return wxBitmap( [[UIImage imageNamed:cfname.AsNSString()] CGImage] ); | |
379 | #else | |
380 | return wxBitmap(); | |
381 | #endif | |
382 | } | |
383 | ||
bc7a3cee SC |
384 | double wxOSXGetMainScreenContentScaleFactor() |
385 | { | |
386 | double scale; | |
387 | ||
388 | #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000 | |
389 | if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) | |
390 | { | |
391 | scale=[[UIScreen mainScreen] scale]; | |
392 | } | |
393 | else | |
394 | #endif | |
395 | { | |
396 | scale=1.0; | |
397 | } | |
398 | ||
399 | return scale; | |
400 | } | |
401 | ||
402 | #endif | |
403 | ||
404 | #if wxOSX_USE_CARBON | |
405 | ||
406 | double wxOSXGetMainScreenContentScaleFactor() | |
407 | { | |
408 | return 1.0; | |
409 | } | |
410 | ||
cf4ce62c SC |
411 | #endif |
412 | ||
f1c40652 SC |
413 | #if wxOSX_USE_COCOA |
414 | ||
e7794cf2 | 415 | wxBitmap wxOSXCreateSystemBitmap(const wxString& name, const wxString &WXUNUSED(client), const wxSize& WXUNUSED(size)) |
ca9eebc3 SC |
416 | { |
417 | wxCFStringRef cfname(name); | |
c944835a | 418 | return wxBitmap( [NSImage imageNamed:cfname.AsNSString()] ); |
ca9eebc3 SC |
419 | } |
420 | ||
489468fe | 421 | // From "Cocoa Drawing Guide:Working with Images" |
c944835a | 422 | WX_NSImage wxOSXGetNSImageFromCGImage( CGImageRef image, double scaleFactor ) |
489468fe SC |
423 | { |
424 | NSRect imageRect = NSMakeRect(0.0, 0.0, 0.0, 0.0); | |
03647350 | 425 | |
489468fe | 426 | // Get the image dimensions. |
c944835a SC |
427 | imageRect.size.height = CGImageGetHeight(image)/scaleFactor; |
428 | imageRect.size.width = CGImageGetWidth(image)/scaleFactor; | |
03647350 | 429 | |
489468fe | 430 | // Create a new image to receive the Quartz image data. |
03647350 | 431 | NSImage *newImage = [[NSImage alloc] initWithSize:imageRect.size]; |
489468fe | 432 | [newImage lockFocus]; |
03647350 | 433 | |
489468fe SC |
434 | // Get the Quartz context and draw. |
435 | CGContextRef imageContext = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort]; | |
436 | CGContextDrawImage( imageContext, *(CGRect*)&imageRect, image ); | |
437 | [newImage unlockFocus]; | |
03647350 | 438 | |
524c47aa SC |
439 | /* |
440 | // Create a bitmap rep from the image... | |
441 | NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:cgImage]; | |
442 | // Create an NSImage and add the bitmap rep to it... | |
443 | NSImage *image = [[NSImage alloc] init]; | |
444 | [image addRepresentation:bitmapRep]; | |
445 | [bitmapRep release]; | |
446 | */ | |
447 | [newImage autorelease]; | |
489468fe SC |
448 | return( newImage ); |
449 | } | |
450 | ||
c944835a | 451 | CGContextRef WXDLLIMPEXP_CORE wxOSXCreateBitmapContextFromNSImage( WX_NSImage nsimage) |
1821877f SC |
452 | { |
453 | // based on http://www.mail-archive.com/cocoa-dev@lists.apple.com/msg18065.html | |
c944835a SC |
454 | |
455 | CGContextRef hbitmap = NULL; | |
89a5da7c SC |
456 | if (nsimage != nil) |
457 | { | |
9c4672b0 | 458 | double scale = wxOSXGetMainScreenContentScaleFactor(); |
c944835a | 459 | |
89a5da7c | 460 | NSSize imageSize = [nsimage size]; |
c944835a SC |
461 | |
462 | hbitmap = CGBitmapContextCreate(NULL, imageSize.width*scale, imageSize.height*scale, 8, 0, wxMacGetGenericRGBColorSpace(), kCGImageAlphaPremultipliedFirst); | |
463 | CGContextScaleCTM( hbitmap, scale, scale ); | |
464 | ||
465 | NSGraphicsContext *nsGraphicsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:hbitmap flipped:NO]; | |
89a5da7c SC |
466 | [NSGraphicsContext saveGraphicsState]; |
467 | [NSGraphicsContext setCurrentContext:nsGraphicsContext]; | |
468 | [[NSColor whiteColor] setFill]; | |
469 | NSRectFill(NSMakeRect(0.0, 0.0, imageSize.width, imageSize.height)); | |
470 | [nsimage drawAtPoint:NSZeroPoint fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0]; | |
471 | [NSGraphicsContext setCurrentContext:nsGraphicsContext]; | |
c944835a SC |
472 | } |
473 | return hbitmap; | |
474 | } | |
475 | ||
bc7a3cee | 476 | double wxOSXGetMainScreenContentScaleFactor() |
9c4672b0 SC |
477 | { |
478 | #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7) | |
479 | if ( [ [NSScreen mainScreen] respondsToSelector:@selector(backingScaleFactor)] ) | |
480 | return [[NSScreen mainScreen] backingScaleFactor]; | |
481 | else | |
482 | #endif | |
483 | return 1.0; | |
484 | } | |
485 | ||
c944835a SC |
486 | CGImageRef wxOSXCreateCGImageFromNSImage( WX_NSImage nsimage, double *scaleptr ) |
487 | { | |
488 | // based on http://www.mail-archive.com/cocoa-dev@lists.apple.com/msg18065.html | |
489 | ||
490 | CGImageRef image = NULL; | |
491 | if (nsimage != nil) | |
492 | { | |
493 | CGContextRef context = wxOSXCreateBitmapContextFromNSImage(nsimage); | |
494 | if ( scaleptr ) | |
495 | { | |
496 | // determine content scale | |
497 | CGRect userrect = CGRectMake(0, 0, 10, 10); | |
498 | CGRect devicerect; | |
499 | devicerect = CGContextConvertRectToDeviceSpace(context, userrect); | |
500 | *scaleptr = devicerect.size.height / userrect.size.height; | |
501 | } | |
89a5da7c SC |
502 | image = CGBitmapContextCreateImage(context); |
503 | CFRelease(context); | |
504 | } | |
1821877f SC |
505 | return image; |
506 | } | |
507 | ||
489468fe SC |
508 | // ---------------------------------------------------------------------------- |
509 | // NSCursor Utils | |
510 | // ---------------------------------------------------------------------------- | |
511 | ||
489468fe SC |
512 | // copied from cursor.mm |
513 | ||
514 | static NSCursor* wxGetStockCursor( short sIndex ) | |
515 | { | |
516 | ClassicCursor* pCursor = &gMacCursors[sIndex]; | |
517 | ||
518 | //Classic mac cursors are 1bps 16x16 black and white with a | |
519 | //identical mask that is 1 for on and 0 for off | |
520 | NSImage *theImage = [[NSImage alloc] initWithSize:NSMakeSize(16.0,16.0)]; | |
521 | ||
522 | //NSCursor takes an NSImage takes a number of Representations - here | |
523 | //we need only one for the raw data | |
536bfe3d SC |
524 | NSBitmapImageRep *theRep = [[NSBitmapImageRep alloc] |
525 | initWithBitmapDataPlanes: NULL // Tell Cocoa to allocate the planes for us. | |
526 | pixelsWide: 16 // All classic cursors are 16x16 | |
527 | pixelsHigh: 16 | |
528 | bitsPerSample: 1 // All classic cursors are bitmaps with bitmasks | |
529 | samplesPerPixel: 2 // Sample 0:image 1:mask | |
530 | hasAlpha: YES // Identify last sample as a mask | |
531 | isPlanar: YES // Use a separate array for each sample | |
532 | colorSpaceName: NSCalibratedWhiteColorSpace // 0.0=black 1.0=white | |
533 | bytesPerRow: 2 // Rows in each plane are on 2-byte boundaries (no pad) | |
534 | bitsPerPixel: 1]; // same as bitsPerSample since data is planar | |
535 | ||
536 | // Ensure that Cocoa allocated 2 and only 2 of the 5 possible planes | |
537 | unsigned char *planes[5]; | |
538 | [theRep getBitmapDataPlanes:planes]; | |
539 | wxASSERT(planes[0] != NULL); | |
540 | wxASSERT(planes[1] != NULL); | |
541 | wxASSERT(planes[2] == NULL); | |
542 | wxASSERT(planes[3] == NULL); | |
543 | wxASSERT(planes[4] == NULL); | |
544 | ||
545 | // NOTE1: The Cursor's bits field is white=0 black=1.. thus the bitwise-not | |
546 | // Why not use NSCalibratedBlackColorSpace? Because that reverses the | |
547 | // sense of the alpha (mask) plane. | |
548 | // NOTE2: The mask data is 0=off 1=on | |
549 | // NOTE3: Cocoa asks for "premultiplied" color planes. Since we have a | |
550 | // 1-bit color plane and a 1-bit alpha plane we can just do a bitwise-and | |
551 | // on the two. The original cursor bitmaps have 0 (white actually) for | |
552 | // any masked-off pixels. Therefore every masked-off pixel would be wrong | |
553 | // since we bit-flip all of the picture bits. In practice, Cocoa doesn't | |
554 | // seem to care, but we are following the documentation. | |
555 | ||
556 | // Fill in the color (black/white) plane | |
557 | for(int i=0; i<16; ++i) | |
489468fe | 558 | { |
536bfe3d SC |
559 | planes[0][2*i ] = (~pCursor->bits[i] & pCursor->mask[i]) >> 8 & 0xff; |
560 | planes[0][2*i+1] = (~pCursor->bits[i] & pCursor->mask[i]) & 0xff; | |
561 | } | |
562 | // Fill in the alpha (i.e. mask) plane | |
563 | for(int i=0; i<16; ++i) | |
564 | { | |
565 | planes[1][2*i ] = pCursor->mask[i] >> 8 & 0xff; | |
566 | planes[1][2*i+1] = pCursor->mask[i] & 0xff; | |
489468fe SC |
567 | } |
568 | ||
569 | //add the representation (data) to the image | |
570 | [theImage addRepresentation:theRep]; | |
571 | ||
572 | //create the new cursor | |
573 | NSCursor* theCursor = [[NSCursor alloc] initWithImage:theImage | |
574 | hotSpot:NSMakePoint(pCursor->hotspot[1], pCursor->hotspot[0]) | |
575 | ]; | |
576 | ||
577 | //do the usual cleanups | |
578 | [theRep release]; | |
579 | [theImage release]; | |
580 | ||
581 | //return the new cursor | |
582 | return theCursor; | |
583 | } | |
584 | ||
585 | WX_NSCursor wxMacCocoaCreateStockCursor( int cursor_type ) | |
586 | { | |
587 | WX_NSCursor cursor = nil; | |
588 | switch (cursor_type) | |
589 | { | |
590 | case wxCURSOR_COPY_ARROW: | |
591 | cursor = [[NSCursor arrowCursor] retain]; | |
592 | break; | |
593 | ||
594 | case wxCURSOR_WATCH: | |
595 | case wxCURSOR_WAIT: | |
4c00ce28 SC |
596 | // an arrow should be displayed by the system when things are running |
597 | // according to the HIG | |
598 | // cursor = [[NSCursor arrowCursor] retain]; | |
599 | // but for crossplatform compatibility we display a watch cursor | |
600 | cursor = wxGetStockCursor(kwxCursorWatch); | |
489468fe SC |
601 | break; |
602 | ||
603 | case wxCURSOR_IBEAM: | |
604 | cursor = [[NSCursor IBeamCursor] retain]; | |
605 | break; | |
606 | ||
607 | case wxCURSOR_CROSS: | |
608 | cursor = [[NSCursor crosshairCursor] retain]; | |
609 | break; | |
610 | ||
611 | case wxCURSOR_SIZENWSE: | |
612 | cursor = wxGetStockCursor(kwxCursorSizeNWSE); | |
613 | break; | |
614 | ||
615 | case wxCURSOR_SIZENESW: | |
616 | cursor = wxGetStockCursor(kwxCursorSizeNESW); | |
617 | break; | |
618 | ||
619 | case wxCURSOR_SIZEWE: | |
620 | cursor = [[NSCursor resizeLeftRightCursor] retain]; | |
621 | break; | |
622 | ||
623 | case wxCURSOR_SIZENS: | |
624 | cursor = [[NSCursor resizeUpDownCursor] retain]; | |
625 | break; | |
626 | ||
627 | case wxCURSOR_SIZING: | |
628 | cursor = wxGetStockCursor(kwxCursorSize); | |
629 | break; | |
630 | ||
631 | case wxCURSOR_HAND: | |
632 | cursor = [[NSCursor pointingHandCursor] retain]; | |
633 | break; | |
03647350 | 634 | |
489468fe SC |
635 | case wxCURSOR_BULLSEYE: |
636 | cursor = wxGetStockCursor(kwxCursorBullseye); | |
637 | break; | |
638 | ||
639 | case wxCURSOR_PENCIL: | |
640 | cursor = wxGetStockCursor(kwxCursorPencil); | |
641 | break; | |
642 | ||
643 | case wxCURSOR_MAGNIFIER: | |
644 | cursor = wxGetStockCursor(kwxCursorMagnifier); | |
645 | break; | |
646 | ||
647 | case wxCURSOR_NO_ENTRY: | |
648 | cursor = wxGetStockCursor(kwxCursorNoEntry); | |
649 | break; | |
650 | ||
651 | case wxCURSOR_PAINT_BRUSH: | |
652 | cursor = wxGetStockCursor(kwxCursorPaintBrush); | |
653 | break; | |
654 | ||
655 | case wxCURSOR_POINT_LEFT: | |
656 | cursor = wxGetStockCursor(kwxCursorPointLeft); | |
657 | break; | |
658 | ||
659 | case wxCURSOR_POINT_RIGHT: | |
660 | cursor = wxGetStockCursor(kwxCursorPointRight); | |
661 | break; | |
662 | ||
663 | case wxCURSOR_QUESTION_ARROW: | |
664 | cursor = wxGetStockCursor(kwxCursorQuestionArrow); | |
665 | break; | |
666 | ||
667 | case wxCURSOR_BLANK: | |
668 | cursor = wxGetStockCursor(kwxCursorBlank); | |
669 | break; | |
670 | ||
671 | case wxCURSOR_RIGHT_ARROW: | |
672 | cursor = wxGetStockCursor(kwxCursorRightArrow); | |
673 | break; | |
674 | ||
675 | case wxCURSOR_SPRAYCAN: | |
676 | cursor = wxGetStockCursor(kwxCursorRoller); | |
677 | break; | |
678 | ||
179c2d91 SC |
679 | case wxCURSOR_OPEN_HAND: |
680 | cursor = [[NSCursor openHandCursor] retain]; | |
681 | break; | |
682 | ||
683 | case wxCURSOR_CLOSED_HAND: | |
684 | cursor = [[NSCursor closedHandCursor] retain]; | |
685 | break; | |
686 | ||
489468fe SC |
687 | case wxCURSOR_CHAR: |
688 | case wxCURSOR_ARROW: | |
689 | case wxCURSOR_LEFT_BUTTON: | |
690 | case wxCURSOR_RIGHT_BUTTON: | |
691 | case wxCURSOR_MIDDLE_BUTTON: | |
692 | default: | |
693 | cursor = [[NSCursor arrowCursor] retain]; | |
694 | break; | |
695 | } | |
696 | return cursor; | |
697 | } | |
698 | ||
699 | // C-based style wrapper routines around NSCursor | |
700 | WX_NSCursor wxMacCocoaCreateCursorFromCGImage( CGImageRef cgImageRef, float hotSpotX, float hotSpotY ) | |
701 | { | |
702 | static BOOL firstTime = YES; | |
03647350 | 703 | |
489468fe SC |
704 | if ( firstTime ) |
705 | { | |
706 | // 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 | |
707 | [[[NSWindow alloc] init] release]; | |
708 | firstTime = NO; | |
709 | } | |
03647350 | 710 | |
59d866ad | 711 | NSImage *nsImage = wxOSXGetNSImageFromCGImage( cgImageRef ); |
489468fe | 712 | NSCursor *cursor = [[NSCursor alloc] initWithImage:nsImage hotSpot:NSMakePoint( hotSpotX, hotSpotY )]; |
03647350 | 713 | |
489468fe SC |
714 | return cursor; |
715 | } | |
716 | ||
717 | void wxMacCocoaSetCursor( WX_NSCursor cursor ) | |
718 | { | |
719 | [cursor set]; | |
720 | } | |
721 | ||
722 | void wxMacCocoaHideCursor() | |
723 | { | |
724 | [NSCursor hide]; | |
725 | } | |
726 | ||
727 | void wxMacCocoaShowCursor() | |
728 | { | |
729 | [NSCursor unhide]; | |
730 | } | |
731 | ||
732 | #endif | |
b2680ced | 733 |