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