]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/osx/carbon/utilscocoa.mm | |
3 | // Purpose: various cocoa mixin utility functions | |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 1998-01-01 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Stefan Csomor | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #ifndef WX_PRECOMP | |
15 | #include "wx/object.h" | |
16 | #endif | |
17 | ||
18 | #if wxOSX_USE_COCOA_OR_CARBON | |
19 | #include <Cocoa/Cocoa.h> | |
20 | #else | |
21 | #import <UIKit/UIKit.h> | |
22 | #endif | |
23 | ||
24 | #ifdef __WXMAC__ | |
25 | #include "wx/osx/private.h" | |
26 | #endif | |
27 | ||
28 | #include "wx/fontutil.h" | |
29 | ||
30 | #if wxOSX_USE_COCOA | |
31 | #include "wx/cocoa/string.h" | |
32 | #endif | |
33 | ||
34 | #ifdef __WXMAC__ | |
35 | ||
36 | #if wxOSX_USE_CARBON | |
37 | bool wxMacInitCocoa() | |
38 | { | |
39 | bool cocoaLoaded = NSApplicationLoad(); | |
40 | wxASSERT_MSG(cocoaLoaded,wxT("Couldn't load Cocoa in Carbon Environment")) ; | |
41 | return cocoaLoaded; | |
42 | } | |
43 | #endif | |
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 | ||
57 | #if wxOSX_USE_COCOA | |
58 | ||
59 | CGContextRef wxOSXGetContextFromCurrentContext() | |
60 | { | |
61 | CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext] | |
62 | graphicsPort]; | |
63 | return context; | |
64 | } | |
65 | ||
66 | bool wxOSXLockFocus( WXWidget view) | |
67 | { | |
68 | return [view lockFocusIfCanDraw]; | |
69 | } | |
70 | ||
71 | void wxOSXUnlockFocus( WXWidget view) | |
72 | { | |
73 | [view unlockFocus]; | |
74 | } | |
75 | ||
76 | #endif | |
77 | ||
78 | #if wxOSX_USE_IPHONE | |
79 | ||
80 | CGContextRef wxOSXGetContextFromCurrentContext() | |
81 | { | |
82 | CGContextRef context = UIGraphicsGetCurrentContext(); | |
83 | return context; | |
84 | } | |
85 | ||
86 | #endif | |
87 | ||
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 | ||
102 | void* wxMacCocoaRetain( void* obj ) | |
103 | { | |
104 | [(NSObject*)obj retain]; | |
105 | return obj; | |
106 | } | |
107 | ||
108 | // ---------------------------------------------------------------------------- | |
109 | // NSFont Utils | |
110 | // ---------------------------------------------------------------------------- | |
111 | ||
112 | #if wxOSX_USE_COCOA | |
113 | wxFont::wxFont(WX_NSFont nsfont) | |
114 | { | |
115 | [nsfont retain]; | |
116 | wxNativeFontInfo info; | |
117 | SetNativeInfoFromNSFont(nsfont, &info); | |
118 | Create(info); | |
119 | } | |
120 | ||
121 | void wxFont::SetNativeInfoFromNSFont(WX_NSFont theFont, wxNativeFontInfo* info) | |
122 | { | |
123 | if ( info->m_faceName.empty()) | |
124 | { | |
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 | ||
134 | wxFontStyle fontstyle = wxFONTSTYLE_NORMAL; | |
135 | wxFontWeight fontweight = wxFONTWEIGHT_NORMAL; | |
136 | bool underlined = false; | |
137 | ||
138 | int size = (int) ([theFont pointSize]+0.5); | |
139 | ||
140 | if ( theFontWeight >= 9 ) | |
141 | fontweight = wxFONTWEIGHT_BOLD ; | |
142 | else if ( theFontWeight < 5 ) | |
143 | fontweight = wxFONTWEIGHT_LIGHT; | |
144 | else | |
145 | fontweight = wxFONTWEIGHT_NORMAL ; | |
146 | ||
147 | if ( theTraits & NSItalicFontMask ) | |
148 | fontstyle = wxFONTSTYLE_ITALIC ; | |
149 | ||
150 | info->Init(size,fontFamily,fontstyle,fontweight,underlined, | |
151 | wxStringWithNSString([theFont familyName]), wxFONTENCODING_DEFAULT); | |
152 | ||
153 | } | |
154 | } | |
155 | ||
156 | WX_NSFont wxFont::OSXCreateNSFont(wxOSXSystemFont font, wxNativeFontInfo* info) | |
157 | { | |
158 | NSFont* nsfont = nil; | |
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: | |
174 | nsfont = [NSFont systemFontOfSize: | |
175 | [NSFont systemFontSizeForControlSize:NSMiniControlSize]]; | |
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: | |
182 | nsfont = [NSFont labelFontOfSize:[NSFont labelFontSize]]; | |
183 | break; | |
184 | case wxOSX_SYSTEM_FONT_VIEWS: | |
185 | nsfont = [NSFont controlContentFontOfSize:0]; | |
186 | break; | |
187 | default: | |
188 | break; | |
189 | } | |
190 | [nsfont retain]; | |
191 | SetNativeInfoFromNSFont(nsfont, info); | |
192 | return nsfont; | |
193 | } | |
194 | ||
195 | static inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; } | |
196 | static const NSAffineTransformStruct kSlantNSTransformStruct = { 1, 0, tan(DegToRad(11)), 1, 0, 0 }; | |
197 | ||
198 | WX_NSFont wxFont::OSXCreateNSFont(const wxNativeFontInfo* info) | |
199 | { | |
200 | NSFont* nsFont; | |
201 | int weight = 5; | |
202 | NSFontTraitMask traits = 0; | |
203 | if (info->m_weight == wxFONTWEIGHT_BOLD) | |
204 | { | |
205 | traits |= NSBoldFontMask; | |
206 | weight = 9; | |
207 | } | |
208 | else if (info->m_weight == wxFONTWEIGHT_LIGHT) | |
209 | weight = 3; | |
210 | ||
211 | if (info->m_style == wxFONTSTYLE_ITALIC || info->m_style == wxFONTSTYLE_SLANT) | |
212 | traits |= NSItalicFontMask; | |
213 | ||
214 | nsFont = [[NSFontManager sharedFontManager] fontWithFamily:wxCFStringRef(info->m_faceName).AsNSString() | |
215 | traits:traits weight:weight size:info->m_pointSize]; | |
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 | } | |
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 ) | |
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 | } | |
271 | #endif | |
272 | if ( nsFontWithTraits != nil ) | |
273 | nsFont = nsFontWithTraits; | |
274 | } | |
275 | } | |
276 | ||
277 | wxASSERT_MSG(nsFont != nil,wxT("Couldn't create nsFont")) ; | |
278 | wxMacCocoaRetain(nsFont); | |
279 | return nsFont; | |
280 | } | |
281 | ||
282 | #endif | |
283 | ||
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; | |
318 | ||
319 | int size = (int) ([uifont pointSize]+0.5); | |
320 | /* | |
321 | NSFontSymbolicTraits traits = [desc symbolicTraits]; | |
322 | ||
323 | if ( traits & NSFontBoldTrait ) | |
324 | fontweight = wxFONTWEIGHT_BOLD ; | |
325 | else | |
326 | fontweight = wxFONTWEIGHT_NORMAL ; | |
327 | if ( traits & NSFontItalicTrait ) | |
328 | fontstyle = wxFONTSTYLE_ITALIC ; | |
329 | */ | |
330 | wxCFStringRef fontname( wxCFRetain([uifont familyName]) ); | |
331 | info->Init(size,wxFONTFAMILY_DEFAULT,fontstyle,fontweight,underlined, | |
332 | fontname.AsString(), wxFONTENCODING_DEFAULT); | |
333 | ||
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 | ||
346 | #endif | |
347 | // ---------------------------------------------------------------------------- | |
348 | // NSImage Utils | |
349 | // ---------------------------------------------------------------------------- | |
350 | ||
351 | #if wxOSX_USE_IPHONE | |
352 | ||
353 | WX_UIImage wxOSXGetUIImageFromCGImage( CGImageRef image ) | |
354 | { | |
355 | UIImage *newImage = [UIImage imageWithCGImage:image]; | |
356 | [newImage autorelease]; | |
357 | return( newImage ); | |
358 | } | |
359 | ||
360 | wxBitmap wxOSXCreateSystemBitmap(const wxString& name, const wxString &client, const wxSize& size) | |
361 | { | |
362 | #if 1 | |
363 | // unfortunately this only accesses images in the app bundle, not the system wide globals | |
364 | wxCFStringRef cfname(name); | |
365 | return wxBitmap( [[UIImage imageNamed:cfname.AsNSString()] CGImage] ); | |
366 | #else | |
367 | return wxBitmap(); | |
368 | #endif | |
369 | } | |
370 | ||
371 | #endif | |
372 | ||
373 | #if wxOSX_USE_COCOA | |
374 | ||
375 | wxBitmap wxOSXCreateSystemBitmap(const wxString& name, const wxString &WXUNUSED(client), const wxSize& WXUNUSED(size)) | |
376 | { | |
377 | wxCFStringRef cfname(name); | |
378 | wxCFRef<CGImageRef> image( wxOSXCreateCGImageFromNSImage([NSImage imageNamed:cfname.AsNSString()]) ); | |
379 | return wxBitmap( image ); | |
380 | } | |
381 | ||
382 | // From "Cocoa Drawing Guide:Working with Images" | |
383 | WX_NSImage wxOSXGetNSImageFromCGImage( CGImageRef image ) | |
384 | { | |
385 | NSRect imageRect = NSMakeRect(0.0, 0.0, 0.0, 0.0); | |
386 | ||
387 | // Get the image dimensions. | |
388 | imageRect.size.height = CGImageGetHeight(image); | |
389 | imageRect.size.width = CGImageGetWidth(image); | |
390 | ||
391 | // Create a new image to receive the Quartz image data. | |
392 | NSImage *newImage = [[NSImage alloc] initWithSize:imageRect.size]; | |
393 | [newImage lockFocus]; | |
394 | ||
395 | // Get the Quartz context and draw. | |
396 | CGContextRef imageContext = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort]; | |
397 | CGContextDrawImage( imageContext, *(CGRect*)&imageRect, image ); | |
398 | [newImage unlockFocus]; | |
399 | ||
400 | /* | |
401 | // Create a bitmap rep from the image... | |
402 | NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:cgImage]; | |
403 | // Create an NSImage and add the bitmap rep to it... | |
404 | NSImage *image = [[NSImage alloc] init]; | |
405 | [image addRepresentation:bitmapRep]; | |
406 | [bitmapRep release]; | |
407 | */ | |
408 | [newImage autorelease]; | |
409 | return( newImage ); | |
410 | } | |
411 | ||
412 | CGImageRef wxOSXCreateCGImageFromNSImage( WX_NSImage nsimage ) | |
413 | { | |
414 | // based on http://www.mail-archive.com/cocoa-dev@lists.apple.com/msg18065.html | |
415 | ||
416 | CGImageRef image = NULL; | |
417 | if (nsimage != nil) | |
418 | { | |
419 | NSSize imageSize = [nsimage size]; | |
420 | CGContextRef context = CGBitmapContextCreate(NULL, imageSize.width, imageSize.height, 8, 0, wxMacGetGenericRGBColorSpace(), kCGImageAlphaPremultipliedFirst); | |
421 | NSGraphicsContext *nsGraphicsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO]; | |
422 | [NSGraphicsContext saveGraphicsState]; | |
423 | [NSGraphicsContext setCurrentContext:nsGraphicsContext]; | |
424 | [[NSColor whiteColor] setFill]; | |
425 | NSRectFill(NSMakeRect(0.0, 0.0, imageSize.width, imageSize.height)); | |
426 | [nsimage drawAtPoint:NSZeroPoint fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0]; | |
427 | [NSGraphicsContext setCurrentContext:nsGraphicsContext]; | |
428 | image = CGBitmapContextCreateImage(context); | |
429 | CFRelease(context); | |
430 | } | |
431 | return image; | |
432 | } | |
433 | ||
434 | // ---------------------------------------------------------------------------- | |
435 | // NSCursor Utils | |
436 | // ---------------------------------------------------------------------------- | |
437 | ||
438 | // copied from cursor.mm | |
439 | ||
440 | static NSCursor* wxGetStockCursor( short sIndex ) | |
441 | { | |
442 | ClassicCursor* pCursor = &gMacCursors[sIndex]; | |
443 | ||
444 | //Classic mac cursors are 1bps 16x16 black and white with a | |
445 | //identical mask that is 1 for on and 0 for off | |
446 | NSImage *theImage = [[NSImage alloc] initWithSize:NSMakeSize(16.0,16.0)]; | |
447 | ||
448 | //NSCursor takes an NSImage takes a number of Representations - here | |
449 | //we need only one for the raw data | |
450 | NSBitmapImageRep *theRep = [[NSBitmapImageRep alloc] | |
451 | initWithBitmapDataPlanes: NULL // Tell Cocoa to allocate the planes for us. | |
452 | pixelsWide: 16 // All classic cursors are 16x16 | |
453 | pixelsHigh: 16 | |
454 | bitsPerSample: 1 // All classic cursors are bitmaps with bitmasks | |
455 | samplesPerPixel: 2 // Sample 0:image 1:mask | |
456 | hasAlpha: YES // Identify last sample as a mask | |
457 | isPlanar: YES // Use a separate array for each sample | |
458 | colorSpaceName: NSCalibratedWhiteColorSpace // 0.0=black 1.0=white | |
459 | bytesPerRow: 2 // Rows in each plane are on 2-byte boundaries (no pad) | |
460 | bitsPerPixel: 1]; // same as bitsPerSample since data is planar | |
461 | ||
462 | // Ensure that Cocoa allocated 2 and only 2 of the 5 possible planes | |
463 | unsigned char *planes[5]; | |
464 | [theRep getBitmapDataPlanes:planes]; | |
465 | wxASSERT(planes[0] != NULL); | |
466 | wxASSERT(planes[1] != NULL); | |
467 | wxASSERT(planes[2] == NULL); | |
468 | wxASSERT(planes[3] == NULL); | |
469 | wxASSERT(planes[4] == NULL); | |
470 | ||
471 | // NOTE1: The Cursor's bits field is white=0 black=1.. thus the bitwise-not | |
472 | // Why not use NSCalibratedBlackColorSpace? Because that reverses the | |
473 | // sense of the alpha (mask) plane. | |
474 | // NOTE2: The mask data is 0=off 1=on | |
475 | // NOTE3: Cocoa asks for "premultiplied" color planes. Since we have a | |
476 | // 1-bit color plane and a 1-bit alpha plane we can just do a bitwise-and | |
477 | // on the two. The original cursor bitmaps have 0 (white actually) for | |
478 | // any masked-off pixels. Therefore every masked-off pixel would be wrong | |
479 | // since we bit-flip all of the picture bits. In practice, Cocoa doesn't | |
480 | // seem to care, but we are following the documentation. | |
481 | ||
482 | // Fill in the color (black/white) plane | |
483 | for(int i=0; i<16; ++i) | |
484 | { | |
485 | planes[0][2*i ] = (~pCursor->bits[i] & pCursor->mask[i]) >> 8 & 0xff; | |
486 | planes[0][2*i+1] = (~pCursor->bits[i] & pCursor->mask[i]) & 0xff; | |
487 | } | |
488 | // Fill in the alpha (i.e. mask) plane | |
489 | for(int i=0; i<16; ++i) | |
490 | { | |
491 | planes[1][2*i ] = pCursor->mask[i] >> 8 & 0xff; | |
492 | planes[1][2*i+1] = pCursor->mask[i] & 0xff; | |
493 | } | |
494 | ||
495 | //add the representation (data) to the image | |
496 | [theImage addRepresentation:theRep]; | |
497 | ||
498 | //create the new cursor | |
499 | NSCursor* theCursor = [[NSCursor alloc] initWithImage:theImage | |
500 | hotSpot:NSMakePoint(pCursor->hotspot[1], pCursor->hotspot[0]) | |
501 | ]; | |
502 | ||
503 | //do the usual cleanups | |
504 | [theRep release]; | |
505 | [theImage release]; | |
506 | ||
507 | //return the new cursor | |
508 | return theCursor; | |
509 | } | |
510 | ||
511 | WX_NSCursor wxMacCocoaCreateStockCursor( int cursor_type ) | |
512 | { | |
513 | WX_NSCursor cursor = nil; | |
514 | switch (cursor_type) | |
515 | { | |
516 | case wxCURSOR_COPY_ARROW: | |
517 | cursor = [[NSCursor arrowCursor] retain]; | |
518 | break; | |
519 | ||
520 | case wxCURSOR_WATCH: | |
521 | case wxCURSOR_WAIT: | |
522 | // an arrow should be displayed by the system when things are running | |
523 | // according to the HIG | |
524 | // cursor = [[NSCursor arrowCursor] retain]; | |
525 | // but for crossplatform compatibility we display a watch cursor | |
526 | cursor = wxGetStockCursor(kwxCursorWatch); | |
527 | break; | |
528 | ||
529 | case wxCURSOR_IBEAM: | |
530 | cursor = [[NSCursor IBeamCursor] retain]; | |
531 | break; | |
532 | ||
533 | case wxCURSOR_CROSS: | |
534 | cursor = [[NSCursor crosshairCursor] retain]; | |
535 | break; | |
536 | ||
537 | case wxCURSOR_SIZENWSE: | |
538 | cursor = wxGetStockCursor(kwxCursorSizeNWSE); | |
539 | break; | |
540 | ||
541 | case wxCURSOR_SIZENESW: | |
542 | cursor = wxGetStockCursor(kwxCursorSizeNESW); | |
543 | break; | |
544 | ||
545 | case wxCURSOR_SIZEWE: | |
546 | cursor = [[NSCursor resizeLeftRightCursor] retain]; | |
547 | break; | |
548 | ||
549 | case wxCURSOR_SIZENS: | |
550 | cursor = [[NSCursor resizeUpDownCursor] retain]; | |
551 | break; | |
552 | ||
553 | case wxCURSOR_SIZING: | |
554 | cursor = wxGetStockCursor(kwxCursorSize); | |
555 | break; | |
556 | ||
557 | case wxCURSOR_HAND: | |
558 | cursor = [[NSCursor pointingHandCursor] retain]; | |
559 | break; | |
560 | ||
561 | case wxCURSOR_BULLSEYE: | |
562 | cursor = wxGetStockCursor(kwxCursorBullseye); | |
563 | break; | |
564 | ||
565 | case wxCURSOR_PENCIL: | |
566 | cursor = wxGetStockCursor(kwxCursorPencil); | |
567 | break; | |
568 | ||
569 | case wxCURSOR_MAGNIFIER: | |
570 | cursor = wxGetStockCursor(kwxCursorMagnifier); | |
571 | break; | |
572 | ||
573 | case wxCURSOR_NO_ENTRY: | |
574 | cursor = wxGetStockCursor(kwxCursorNoEntry); | |
575 | break; | |
576 | ||
577 | case wxCURSOR_PAINT_BRUSH: | |
578 | cursor = wxGetStockCursor(kwxCursorPaintBrush); | |
579 | break; | |
580 | ||
581 | case wxCURSOR_POINT_LEFT: | |
582 | cursor = wxGetStockCursor(kwxCursorPointLeft); | |
583 | break; | |
584 | ||
585 | case wxCURSOR_POINT_RIGHT: | |
586 | cursor = wxGetStockCursor(kwxCursorPointRight); | |
587 | break; | |
588 | ||
589 | case wxCURSOR_QUESTION_ARROW: | |
590 | cursor = wxGetStockCursor(kwxCursorQuestionArrow); | |
591 | break; | |
592 | ||
593 | case wxCURSOR_BLANK: | |
594 | cursor = wxGetStockCursor(kwxCursorBlank); | |
595 | break; | |
596 | ||
597 | case wxCURSOR_RIGHT_ARROW: | |
598 | cursor = wxGetStockCursor(kwxCursorRightArrow); | |
599 | break; | |
600 | ||
601 | case wxCURSOR_SPRAYCAN: | |
602 | cursor = wxGetStockCursor(kwxCursorRoller); | |
603 | break; | |
604 | ||
605 | case wxCURSOR_OPEN_HAND: | |
606 | cursor = [[NSCursor openHandCursor] retain]; | |
607 | break; | |
608 | ||
609 | case wxCURSOR_CLOSED_HAND: | |
610 | cursor = [[NSCursor closedHandCursor] retain]; | |
611 | break; | |
612 | ||
613 | case wxCURSOR_CHAR: | |
614 | case wxCURSOR_ARROW: | |
615 | case wxCURSOR_LEFT_BUTTON: | |
616 | case wxCURSOR_RIGHT_BUTTON: | |
617 | case wxCURSOR_MIDDLE_BUTTON: | |
618 | default: | |
619 | cursor = [[NSCursor arrowCursor] retain]; | |
620 | break; | |
621 | } | |
622 | return cursor; | |
623 | } | |
624 | ||
625 | // C-based style wrapper routines around NSCursor | |
626 | WX_NSCursor wxMacCocoaCreateCursorFromCGImage( CGImageRef cgImageRef, float hotSpotX, float hotSpotY ) | |
627 | { | |
628 | static BOOL firstTime = YES; | |
629 | ||
630 | if ( firstTime ) | |
631 | { | |
632 | // 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 | |
633 | [[[NSWindow alloc] init] release]; | |
634 | firstTime = NO; | |
635 | } | |
636 | ||
637 | NSImage *nsImage = wxOSXGetNSImageFromCGImage( cgImageRef ); | |
638 | NSCursor *cursor = [[NSCursor alloc] initWithImage:nsImage hotSpot:NSMakePoint( hotSpotX, hotSpotY )]; | |
639 | ||
640 | return cursor; | |
641 | } | |
642 | ||
643 | void wxMacCocoaSetCursor( WX_NSCursor cursor ) | |
644 | { | |
645 | [cursor set]; | |
646 | } | |
647 | ||
648 | void wxMacCocoaHideCursor() | |
649 | { | |
650 | [NSCursor hide]; | |
651 | } | |
652 | ||
653 | void wxMacCocoaShowCursor() | |
654 | { | |
655 | [NSCursor unhide]; | |
656 | } | |
657 | ||
658 | #endif | |
659 |