]> git.saurik.com Git - wxWidgets.git/blame - src/osx/carbon/utilscocoa.mm
OSXTimer for all variants
[wxWidgets.git] / src / osx / carbon / utilscocoa.mm
CommitLineData
489468fe
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/mac/carbon/utils.mm
3// Purpose: various cocoa mixin utility functions
4// Author: Stefan Csomor
5// Modified by:
6// Created: 1998-01-01
7// RCS-ID: $Id: utilscocoa.mm 48805 2007-09-19 14:52:25Z SC $
8// Copyright: (c) Stefan Csomor
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
13
b2680ced 14#if wxOSX_USE_COCOA_OR_CARBON
489468fe 15#include <Cocoa/Cocoa.h>
b2680ced
SC
16#else
17#import <UIKit/UIKit.h>
18#endif
489468fe
SC
19
20#ifdef __WXMAC__
1f0c8f31 21#include "wx/osx/private.h"
489468fe
SC
22#endif
23
24#ifdef __WXMAC__
25
b2680ced 26#if wxOSX_USE_CARBON
489468fe
SC
27bool wxMacInitCocoa()
28{
29 bool cocoaLoaded = NSApplicationLoad();
30 wxASSERT_MSG(cocoaLoaded,wxT("Couldn't load Cocoa in Carbon Environment")) ;
31 return cocoaLoaded;
32}
b2680ced 33#endif
489468fe
SC
34
35wxMacAutoreleasePool::wxMacAutoreleasePool()
36{
37 m_pool = [[NSAutoreleasePool alloc] init];
38}
39
40wxMacAutoreleasePool::~wxMacAutoreleasePool()
41{
42 [(NSAutoreleasePool*)m_pool release];
43}
44
45#endif
46
b2680ced 47#if defined( __WXCOCOCA__ ) || wxOSX_USE_COCOA
489468fe
SC
48
49CGContextRef wxMacGetContextFromCurrentNSContext()
50{
51 CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext]
52 graphicsPort];
53 return context;
54}
55
56#endif
57
58// ----------------------------------------------------------------------------
59// NSObject Utils
60// ----------------------------------------------------------------------------
61
62void wxMacCocoaRelease( void* obj )
63{
64 [(NSObject*)obj release];
65}
66
67void wxMacCocoaAutorelease( void* obj )
68{
69 [(NSObject*)obj autorelease];
70}
71
72void wxMacCocoaRetain( void* obj )
73{
74 [(NSObject*)obj retain];
75}
76
b2680ced
SC
77#if wxOSX_USE_COCOA
78
489468fe
SC
79// ----------------------------------------------------------------------------
80// NSImage Utils
81// ----------------------------------------------------------------------------
82
83// From "Cocoa Drawing Guide:Working with Images"
84WX_NSImage CreateNSImageFromCGImage( CGImageRef image )
85{
86 NSRect imageRect = NSMakeRect(0.0, 0.0, 0.0, 0.0);
87
88 // Get the image dimensions.
89 imageRect.size.height = CGImageGetHeight(image);
90 imageRect.size.width = CGImageGetWidth(image);
91
92 // Create a new image to receive the Quartz image data.
93 NSImage *newImage = [[NSImage alloc] initWithSize:imageRect.size];
94 [newImage lockFocus];
95
96 // Get the Quartz context and draw.
97 CGContextRef imageContext = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
98 CGContextDrawImage( imageContext, *(CGRect*)&imageRect, image );
99 [newImage unlockFocus];
100
101 return( newImage );
102}
103
104// ----------------------------------------------------------------------------
105// NSCursor Utils
106// ----------------------------------------------------------------------------
107
489468fe
SC
108// copied from cursor.mm
109
110static NSCursor* wxGetStockCursor( short sIndex )
111{
112 ClassicCursor* pCursor = &gMacCursors[sIndex];
113
114 //Classic mac cursors are 1bps 16x16 black and white with a
115 //identical mask that is 1 for on and 0 for off
116 NSImage *theImage = [[NSImage alloc] initWithSize:NSMakeSize(16.0,16.0)];
117
118 //NSCursor takes an NSImage takes a number of Representations - here
119 //we need only one for the raw data
120 NSBitmapImageRep *theRep =
121 [[NSBitmapImageRep alloc]
122 initWithBitmapDataPlanes:nil // Allocate the buffer for us :)
123 pixelsWide:16
124 pixelsHigh:16
125 bitsPerSample:1
126 samplesPerPixel:2
127 hasAlpha:YES // Well, more like a mask...
128 isPlanar:NO
129 colorSpaceName:NSCalibratedWhiteColorSpace // Normal B/W - 0 black 1 white
130 bytesPerRow:0 // I don't care - figure it out for me :)
131 bitsPerPixel:2]; // bitsPerSample * samplesPerPixel
132
133 //unsigned int is better to put data in then a void*
134 //note that working with bitfields would be a lot better here -
135 //but since it breaks some compilers...
136 wxUint32 *data = (wxUint32 *)[theRep bitmapData];
137
138 //traverse through the bitmap data
139 for (int i = 0; i < 16; ++i)
140 {
141 //bit alpha bit alpha ... :D
142
143 //Notice the = instead of |= -
144 //this is to avoid doing a memset earlier
145 data[i] = 0;
146
147 //do the rest of those bits and alphas :)
148 for (int shift = 0; shift < 32; ++shift)
149 {
150 const int bit = 1 << (shift >> 1);
151 data[i] |= ( !!( (pCursor->mask[i] & bit) ) ) << shift;
152 data[i] |= ( !( (pCursor->bits[i] & bit) ) ) << ++shift;
153 }
154 }
155
156 //add the representation (data) to the image
157 [theImage addRepresentation:theRep];
158
159 //create the new cursor
160 NSCursor* theCursor = [[NSCursor alloc] initWithImage:theImage
161 hotSpot:NSMakePoint(pCursor->hotspot[1], pCursor->hotspot[0])
162 ];
163
164 //do the usual cleanups
165 [theRep release];
166 [theImage release];
167
168 //return the new cursor
169 return theCursor;
170}
171
172WX_NSCursor wxMacCocoaCreateStockCursor( int cursor_type )
173{
174 WX_NSCursor cursor = nil;
175 switch (cursor_type)
176 {
177 case wxCURSOR_COPY_ARROW:
178 cursor = [[NSCursor arrowCursor] retain];
179 break;
180
181 case wxCURSOR_WATCH:
182 case wxCURSOR_WAIT:
183 // should be displayed by the system when things are running
184 cursor = [[NSCursor arrowCursor] retain];
185 break;
186
187 case wxCURSOR_IBEAM:
188 cursor = [[NSCursor IBeamCursor] retain];
189 break;
190
191 case wxCURSOR_CROSS:
192 cursor = [[NSCursor crosshairCursor] retain];
193 break;
194
195 case wxCURSOR_SIZENWSE:
196 cursor = wxGetStockCursor(kwxCursorSizeNWSE);
197 break;
198
199 case wxCURSOR_SIZENESW:
200 cursor = wxGetStockCursor(kwxCursorSizeNESW);
201 break;
202
203 case wxCURSOR_SIZEWE:
204 cursor = [[NSCursor resizeLeftRightCursor] retain];
205 break;
206
207 case wxCURSOR_SIZENS:
208 cursor = [[NSCursor resizeUpDownCursor] retain];
209 break;
210
211 case wxCURSOR_SIZING:
212 cursor = wxGetStockCursor(kwxCursorSize);
213 break;
214
215 case wxCURSOR_HAND:
216 cursor = [[NSCursor pointingHandCursor] retain];
217 break;
218
219 case wxCURSOR_BULLSEYE:
220 cursor = wxGetStockCursor(kwxCursorBullseye);
221 break;
222
223 case wxCURSOR_PENCIL:
224 cursor = wxGetStockCursor(kwxCursorPencil);
225 break;
226
227 case wxCURSOR_MAGNIFIER:
228 cursor = wxGetStockCursor(kwxCursorMagnifier);
229 break;
230
231 case wxCURSOR_NO_ENTRY:
232 cursor = wxGetStockCursor(kwxCursorNoEntry);
233 break;
234
235 case wxCURSOR_PAINT_BRUSH:
236 cursor = wxGetStockCursor(kwxCursorPaintBrush);
237 break;
238
239 case wxCURSOR_POINT_LEFT:
240 cursor = wxGetStockCursor(kwxCursorPointLeft);
241 break;
242
243 case wxCURSOR_POINT_RIGHT:
244 cursor = wxGetStockCursor(kwxCursorPointRight);
245 break;
246
247 case wxCURSOR_QUESTION_ARROW:
248 cursor = wxGetStockCursor(kwxCursorQuestionArrow);
249 break;
250
251 case wxCURSOR_BLANK:
252 cursor = wxGetStockCursor(kwxCursorBlank);
253 break;
254
255 case wxCURSOR_RIGHT_ARROW:
256 cursor = wxGetStockCursor(kwxCursorRightArrow);
257 break;
258
259 case wxCURSOR_SPRAYCAN:
260 cursor = wxGetStockCursor(kwxCursorRoller);
261 break;
262
263 case wxCURSOR_CHAR:
264 case wxCURSOR_ARROW:
265 case wxCURSOR_LEFT_BUTTON:
266 case wxCURSOR_RIGHT_BUTTON:
267 case wxCURSOR_MIDDLE_BUTTON:
268 default:
269 cursor = [[NSCursor arrowCursor] retain];
270 break;
271 }
272 return cursor;
273}
274
275// C-based style wrapper routines around NSCursor
276WX_NSCursor wxMacCocoaCreateCursorFromCGImage( CGImageRef cgImageRef, float hotSpotX, float hotSpotY )
277{
278 static BOOL firstTime = YES;
279
280 if ( firstTime )
281 {
282 // 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
283 [[[NSWindow alloc] init] release];
284 firstTime = NO;
285 }
286
287 NSImage *nsImage = CreateNSImageFromCGImage( cgImageRef );
288 NSCursor *cursor = [[NSCursor alloc] initWithImage:nsImage hotSpot:NSMakePoint( hotSpotX, hotSpotY )];
289
290 [nsImage release];
291
292 return cursor;
293}
294
295void wxMacCocoaSetCursor( WX_NSCursor cursor )
296{
297 [cursor set];
298}
299
300void wxMacCocoaHideCursor()
301{
302 [NSCursor hide];
303}
304
305void wxMacCocoaShowCursor()
306{
307 [NSCursor unhide];
308}
309
310#endif
b2680ced 311