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