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