]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/utilscocoa.mm
adding more native icons, fixes #8778
[wxWidgets.git] / src / osx / carbon / utilscocoa.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/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 #if wxOSX_USE_COCOA_OR_CARBON
15 #include <Cocoa/Cocoa.h>
16 #else
17 #import <UIKit/UIKit.h>
18 #endif
19
20 #ifdef __WXMAC__
21 #include "wx/osx/private.h"
22 #endif
23
24 #include "wx/fontutil.h"
25
26 #ifdef __WXMAC__
27
28 #if wxOSX_USE_CARBON
29 bool wxMacInitCocoa()
30 {
31 bool cocoaLoaded = NSApplicationLoad();
32 wxASSERT_MSG(cocoaLoaded,wxT("Couldn't load Cocoa in Carbon Environment")) ;
33 return cocoaLoaded;
34 }
35 #endif
36
37 wxMacAutoreleasePool::wxMacAutoreleasePool()
38 {
39 m_pool = [[NSAutoreleasePool alloc] init];
40 }
41
42 wxMacAutoreleasePool::~wxMacAutoreleasePool()
43 {
44 [(NSAutoreleasePool*)m_pool release];
45 }
46
47 #endif
48
49 #if wxOSX_USE_COCOA_OR_IPHONE
50
51 CGContextRef wxOSXGetContextFromCurrentNSContext()
52 {
53 CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext]
54 graphicsPort];
55 return context;
56 }
57
58 bool wxOSXLockFocus( WXWidget view)
59 {
60 return [view lockFocusIfCanDraw];
61 }
62
63 void wxOSXUnlockFocus( WXWidget view)
64 {
65 [view unlockFocus];
66 }
67
68 #endif
69
70 // ----------------------------------------------------------------------------
71 // NSObject Utils
72 // ----------------------------------------------------------------------------
73
74 void wxMacCocoaRelease( void* obj )
75 {
76 [(NSObject*)obj release];
77 }
78
79 void wxMacCocoaAutorelease( void* obj )
80 {
81 [(NSObject*)obj autorelease];
82 }
83
84 void* wxMacCocoaRetain( void* obj )
85 {
86 [(NSObject*)obj retain];
87 return obj;
88 }
89
90 // ----------------------------------------------------------------------------
91 // NSFont Utils
92 // ----------------------------------------------------------------------------
93
94 #if wxOSX_USE_COCOA
95
96 WX_NSFont wxFont::CreateNSFont(wxOSXSystemFont font, wxNativeFontInfo* info)
97 {
98 NSFont* nsfont = nil;
99 switch( font )
100 {
101 case wxOSX_SYSTEM_FONT_NORMAL:
102 nsfont = [NSFont systemFontOfSize:[NSFont systemFontSize]];
103 break;
104 case wxOSX_SYSTEM_FONT_BOLD:
105 nsfont = [NSFont boldSystemFontOfSize:[NSFont systemFontSize]];
106 break;
107 case wxOSX_SYSTEM_FONT_SMALL:
108 nsfont = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]];
109 break;
110 case wxOSX_SYSTEM_FONT_SMALL_BOLD:
111 nsfont = [NSFont boldSystemFontOfSize:[NSFont smallSystemFontSize]];
112 break;
113 case wxOSX_SYSTEM_FONT_MINI:
114 nsfont = [NSFont systemFontOfSize:[NSFont systemFontSize]];
115 break;
116 case wxOSX_SYSTEM_FONT_MINI_BOLD:
117 nsfont = [NSFont boldSystemFontOfSize:
118 [NSFont systemFontSizeForControlSize:NSMiniControlSize]];
119 break;
120 case wxOSX_SYSTEM_FONT_LABELS:
121 nsfont = [NSFont labelFontOfSize:
122 [NSFont systemFontSizeForControlSize:NSMiniControlSize]];
123 break;
124 case wxOSX_SYSTEM_FONT_VIEWS:
125 nsfont = [NSFont controlContentFontOfSize:0];
126 break;
127 default:
128 break;
129 }
130 [nsfont retain];
131 NSFontDescriptor*desc = [[nsfont fontDescriptor] retain];
132 if ( info->m_faceName.empty())
133 {
134 wxFontStyle fontstyle = wxFONTSTYLE_NORMAL;
135 wxFontWeight fontweight = wxFONTWEIGHT_NORMAL;
136 bool underlined = false;
137
138 int size = (int) ([desc pointSize]+0.5);
139 NSFontSymbolicTraits traits = [desc symbolicTraits];
140
141 if ( traits & NSFontBoldTrait )
142 fontweight = wxFONTWEIGHT_BOLD ;
143 else
144 fontweight = wxFONTWEIGHT_NORMAL ;
145 if ( traits & NSFontItalicTrait )
146 fontstyle = wxFONTSTYLE_ITALIC ;
147
148 wxCFStringRef fontname( [desc postscriptName] );
149 info->Init(size,wxFONTFAMILY_DEFAULT,fontstyle,fontweight,underlined,
150 fontname.AsString(), wxFONTENCODING_DEFAULT);
151
152 }
153 info->m_nsFontDescriptor = desc;
154 return nsfont;
155 }
156
157 void wxNativeFontInfo::ValidateNSFontDescriptor()
158 {
159 NSFontDescriptor* desc = [NSFontDescriptor fontDescriptorWithName:wxCFStringRef(m_faceName).AsNSString() size:m_pointSize];
160 NSFontSymbolicTraits traits = 0;
161
162 if (m_weight == wxFONTWEIGHT_BOLD)
163 traits |= NSFontBoldTrait;
164 if (m_style == wxFONTSTYLE_ITALIC || m_style == wxFONTSTYLE_SLANT)
165 traits |= NSFontItalicTrait;
166
167 if ( traits != 0 )
168 {
169 desc = [desc fontDescriptorWithSymbolicTraits:traits];
170 }
171 wxMacCocoaRetain(desc);
172 m_nsFontDescriptor = desc;
173 }
174
175 WX_NSFont wxFont::CreateNSFont(const wxNativeFontInfo* info)
176 {
177 NSFont* nsFont;
178 nsFont = [NSFont fontWithDescriptor:info->m_nsFontDescriptor size:info->m_pointSize];
179 wxMacCocoaRetain(nsFont);
180 return nsFont;
181 }
182
183 #endif
184
185 // ----------------------------------------------------------------------------
186 // NSImage Utils
187 // ----------------------------------------------------------------------------
188
189 #if wxOSX_USE_COCOA
190
191 // From "Cocoa Drawing Guide:Working with Images"
192 WX_NSImage wxOSXCreateNSImageFromCGImage( CGImageRef image )
193 {
194 NSRect imageRect = NSMakeRect(0.0, 0.0, 0.0, 0.0);
195
196 // Get the image dimensions.
197 imageRect.size.height = CGImageGetHeight(image);
198 imageRect.size.width = CGImageGetWidth(image);
199
200 // Create a new image to receive the Quartz image data.
201 NSImage *newImage = [[NSImage alloc] initWithSize:imageRect.size];
202 [newImage lockFocus];
203
204 // Get the Quartz context and draw.
205 CGContextRef imageContext = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
206 CGContextDrawImage( imageContext, *(CGRect*)&imageRect, image );
207 [newImage unlockFocus];
208
209 /*
210 // Create a bitmap rep from the image...
211 NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:cgImage];
212 // Create an NSImage and add the bitmap rep to it...
213 NSImage *image = [[NSImage alloc] init];
214 [image addRepresentation:bitmapRep];
215 [bitmapRep release];
216 */
217 [newImage autorelease];
218 return( newImage );
219 }
220
221 // ----------------------------------------------------------------------------
222 // NSCursor Utils
223 // ----------------------------------------------------------------------------
224
225 // copied from cursor.mm
226
227 static NSCursor* wxGetStockCursor( short sIndex )
228 {
229 ClassicCursor* pCursor = &gMacCursors[sIndex];
230
231 //Classic mac cursors are 1bps 16x16 black and white with a
232 //identical mask that is 1 for on and 0 for off
233 NSImage *theImage = [[NSImage alloc] initWithSize:NSMakeSize(16.0,16.0)];
234
235 //NSCursor takes an NSImage takes a number of Representations - here
236 //we need only one for the raw data
237 NSBitmapImageRep *theRep =
238 [[NSBitmapImageRep alloc]
239 initWithBitmapDataPlanes:nil // Allocate the buffer for us :)
240 pixelsWide:16
241 pixelsHigh:16
242 bitsPerSample:1
243 samplesPerPixel:2
244 hasAlpha:YES // Well, more like a mask...
245 isPlanar:NO
246 colorSpaceName:NSCalibratedWhiteColorSpace // Normal B/W - 0 black 1 white
247 bytesPerRow:0 // I don't care - figure it out for me :)
248 bitsPerPixel:2]; // bitsPerSample * samplesPerPixel
249
250 //unsigned int is better to put data in then a void*
251 //note that working with bitfields would be a lot better here -
252 //but since it breaks some compilers...
253 wxUint32 *data = (wxUint32 *)[theRep bitmapData];
254
255 //traverse through the bitmap data
256 for (int i = 0; i < 16; ++i)
257 {
258 //bit alpha bit alpha ... :D
259
260 //Notice the = instead of |= -
261 //this is to avoid doing a memset earlier
262 data[i] = 0;
263
264 //do the rest of those bits and alphas :)
265 for (int shift = 0; shift < 32; ++shift)
266 {
267 const int bit = 1 << (shift >> 1);
268 data[i] |= ( !!( (pCursor->mask[i] & bit) ) ) << shift;
269 data[i] |= ( !( (pCursor->bits[i] & bit) ) ) << ++shift;
270 }
271 }
272
273 //add the representation (data) to the image
274 [theImage addRepresentation:theRep];
275
276 //create the new cursor
277 NSCursor* theCursor = [[NSCursor alloc] initWithImage:theImage
278 hotSpot:NSMakePoint(pCursor->hotspot[1], pCursor->hotspot[0])
279 ];
280
281 //do the usual cleanups
282 [theRep release];
283 [theImage release];
284
285 //return the new cursor
286 return theCursor;
287 }
288
289 WX_NSCursor wxMacCocoaCreateStockCursor( int cursor_type )
290 {
291 WX_NSCursor cursor = nil;
292 switch (cursor_type)
293 {
294 case wxCURSOR_COPY_ARROW:
295 cursor = [[NSCursor arrowCursor] retain];
296 break;
297
298 case wxCURSOR_WATCH:
299 case wxCURSOR_WAIT:
300 // should be displayed by the system when things are running
301 cursor = [[NSCursor arrowCursor] retain];
302 break;
303
304 case wxCURSOR_IBEAM:
305 cursor = [[NSCursor IBeamCursor] retain];
306 break;
307
308 case wxCURSOR_CROSS:
309 cursor = [[NSCursor crosshairCursor] retain];
310 break;
311
312 case wxCURSOR_SIZENWSE:
313 cursor = wxGetStockCursor(kwxCursorSizeNWSE);
314 break;
315
316 case wxCURSOR_SIZENESW:
317 cursor = wxGetStockCursor(kwxCursorSizeNESW);
318 break;
319
320 case wxCURSOR_SIZEWE:
321 cursor = [[NSCursor resizeLeftRightCursor] retain];
322 break;
323
324 case wxCURSOR_SIZENS:
325 cursor = [[NSCursor resizeUpDownCursor] retain];
326 break;
327
328 case wxCURSOR_SIZING:
329 cursor = wxGetStockCursor(kwxCursorSize);
330 break;
331
332 case wxCURSOR_HAND:
333 cursor = [[NSCursor pointingHandCursor] retain];
334 break;
335
336 case wxCURSOR_BULLSEYE:
337 cursor = wxGetStockCursor(kwxCursorBullseye);
338 break;
339
340 case wxCURSOR_PENCIL:
341 cursor = wxGetStockCursor(kwxCursorPencil);
342 break;
343
344 case wxCURSOR_MAGNIFIER:
345 cursor = wxGetStockCursor(kwxCursorMagnifier);
346 break;
347
348 case wxCURSOR_NO_ENTRY:
349 cursor = wxGetStockCursor(kwxCursorNoEntry);
350 break;
351
352 case wxCURSOR_PAINT_BRUSH:
353 cursor = wxGetStockCursor(kwxCursorPaintBrush);
354 break;
355
356 case wxCURSOR_POINT_LEFT:
357 cursor = wxGetStockCursor(kwxCursorPointLeft);
358 break;
359
360 case wxCURSOR_POINT_RIGHT:
361 cursor = wxGetStockCursor(kwxCursorPointRight);
362 break;
363
364 case wxCURSOR_QUESTION_ARROW:
365 cursor = wxGetStockCursor(kwxCursorQuestionArrow);
366 break;
367
368 case wxCURSOR_BLANK:
369 cursor = wxGetStockCursor(kwxCursorBlank);
370 break;
371
372 case wxCURSOR_RIGHT_ARROW:
373 cursor = wxGetStockCursor(kwxCursorRightArrow);
374 break;
375
376 case wxCURSOR_SPRAYCAN:
377 cursor = wxGetStockCursor(kwxCursorRoller);
378 break;
379
380 case wxCURSOR_OPEN_HAND:
381 cursor = [[NSCursor openHandCursor] retain];
382 break;
383
384 case wxCURSOR_CLOSED_HAND:
385 cursor = [[NSCursor closedHandCursor] retain];
386 break;
387
388 case wxCURSOR_CHAR:
389 case wxCURSOR_ARROW:
390 case wxCURSOR_LEFT_BUTTON:
391 case wxCURSOR_RIGHT_BUTTON:
392 case wxCURSOR_MIDDLE_BUTTON:
393 default:
394 cursor = [[NSCursor arrowCursor] retain];
395 break;
396 }
397 return cursor;
398 }
399
400 // C-based style wrapper routines around NSCursor
401 WX_NSCursor wxMacCocoaCreateCursorFromCGImage( CGImageRef cgImageRef, float hotSpotX, float hotSpotY )
402 {
403 static BOOL firstTime = YES;
404
405 if ( firstTime )
406 {
407 // 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
408 [[[NSWindow alloc] init] release];
409 firstTime = NO;
410 }
411
412 NSImage *nsImage = wxOSXCreateNSImageFromCGImage( cgImageRef );
413 NSCursor *cursor = [[NSCursor alloc] initWithImage:nsImage hotSpot:NSMakePoint( hotSpotX, hotSpotY )];
414
415 [nsImage release];
416
417 return cursor;
418 }
419
420 void wxMacCocoaSetCursor( WX_NSCursor cursor )
421 {
422 [cursor set];
423 }
424
425 void wxMacCocoaHideCursor()
426 {
427 [NSCursor hide];
428 }
429
430 void wxMacCocoaShowCursor()
431 {
432 [NSCursor unhide];
433 }
434
435 #endif
436