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