1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/utils_osx.cpp
3 // Purpose: Various utilities
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #include "wx/wxprec.h"
22 #include "wx/toplevel.h"
27 #include "wx/apptrait.h"
36 // #include "MoreFilesX.h"
38 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
39 #include <AudioToolbox/AudioServices.h>
42 #include "wx/osx/private.h"
43 #include "wx/osx/private/timer.h"
45 #include "wx/evtloop.h"
47 // Check whether this window wants to process messages, e.g. Stop button
48 // in long calculations.
49 bool wxCheckForInterrupt(wxWindow
*WXUNUSED(wnd
))
55 // Return true if we have a colour display
56 bool wxColourDisplay()
58 // always the case on OS X
63 #if wxOSX_USE_COCOA_OR_CARBON
65 #if (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070) && (MAC_OS_X_VERSION_MIN_REQUIRED < 1060)
66 // bring back declaration so that we can support deployment targets < 10_6
67 CG_EXTERN
size_t CGDisplayBitsPerPixel(CGDirectDisplayID display
)
68 CG_AVAILABLE_BUT_DEPRECATED(__MAC_10_0
, __MAC_10_6
,
69 __IPHONE_NA
, __IPHONE_NA
);
72 // Returns depth of screen
77 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
78 if ( UMAGetSystemVersion() >= 0x1060 )
80 CGDisplayModeRef currentMode
= CGDisplayCopyDisplayMode(kCGDirectMainDisplay
);
81 CFStringRef encoding
= CGDisplayModeCopyPixelEncoding(currentMode
);
83 if(CFStringCompare(encoding
, CFSTR(IO32BitDirectPixels
), kCFCompareCaseInsensitive
) == kCFCompareEqualTo
)
85 else if(CFStringCompare(encoding
, CFSTR(IO16BitDirectPixels
), kCFCompareCaseInsensitive
) == kCFCompareEqualTo
)
87 else if(CFStringCompare(encoding
, CFSTR(IO8BitIndexedPixels
), kCFCompareCaseInsensitive
) == kCFCompareEqualTo
)
90 theDepth
= 32; // some reasonable default
93 CGDisplayModeRelease(currentMode
);
98 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1060
99 theDepth
= (int) CGDisplayBitsPerPixel(CGMainDisplayID());
105 // Get size of display
106 void wxDisplaySize(int *width
, int *height
)
108 // TODO adapt for multi-displays
109 CGRect bounds
= CGDisplayBounds(CGMainDisplayID());
111 *width
= (int)bounds
.size
.width
;
113 *height
= (int)bounds
.size
.height
;
118 // ----------------------------------------------------------------------------
119 // Launch document with default app
120 // ----------------------------------------------------------------------------
122 bool wxLaunchDefaultApplication(const wxString
& document
, int flags
)
126 wxCFRef
<CFMutableStringRef
> cfMutableString(CFStringCreateMutableCopy(NULL
, 0, wxCFStringRef(document
)));
127 CFStringNormalize(cfMutableString
,kCFStringNormalizationFormD
);
128 wxCFRef
<CFURLRef
> curl(CFURLCreateWithFileSystemPath(kCFAllocatorDefault
, cfMutableString
, kCFURLPOSIXPathStyle
, false));
129 OSStatus err
= LSOpenCFURLRef( curl
, NULL
);
137 wxLogDebug(wxT("Default Application Launch error %d"), (int) err
);
142 // ----------------------------------------------------------------------------
143 // Launch default browser
144 // ----------------------------------------------------------------------------
146 bool wxDoLaunchDefaultBrowser(const wxString
& url
, int flags
)
149 wxCFRef
< CFURLRef
> curl( CFURLCreateWithString( kCFAllocatorDefault
,
150 wxCFStringRef( url
), NULL
) );
151 OSStatus err
= LSOpenCFURLRef( curl
, NULL
);
159 wxLogDebug(wxT("Browser Launch error %d"), (int) err
);
168 void wxDisplaySizeMM(int *width
, int *height
)
170 wxDisplaySize(width
, height
);
171 // on mac 72 is fixed (at least now;-)
172 double cvPt2Mm
= 25.4 / 72;
175 *width
= int( *width
* cvPt2Mm
);
178 *height
= int( *height
* cvPt2Mm
);
182 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
184 // We suppose that toolkit version is the same as OS version under Mac
185 wxGetOsVersion(verMaj
, verMin
);
190 wxEventLoopBase
* wxGUIAppTraits::CreateEventLoop()
192 return new wxEventLoop
;
195 wxWindow
* wxFindWindowAtPoint(wxWindow
* win
, const wxPoint
& pt
);
197 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
201 Point screenPoint
= { pt
.y
, pt
.x
};
204 if ( FindWindow( screenPoint
, &windowRef
) )
206 wxNonOwnedWindow
*nonOwned
= wxNonOwnedWindow::GetFromWXWindow( windowRef
);
209 return wxFindWindowAtPoint( nonOwned
, pt
);
216 return wxGenericFindWindowAtPoint( pt
);
222 Return the generic RGB color space. This is a 'get' function and the caller should
223 not release the returned value unless the caller retains it first. Usually callers
224 of this routine will immediately use the returned colorspace with CoreGraphics
225 so they typically do not need to retain it themselves.
227 This function creates the generic RGB color space once and hangs onto it so it can
228 return it whenever this function is called.
231 CGColorSpaceRef
wxMacGetGenericRGBColorSpace()
233 static wxCFRef
<CGColorSpaceRef
> genericRGBColorSpace
;
235 if (genericRGBColorSpace
== NULL
)
238 genericRGBColorSpace
.reset( CGColorSpaceCreateDeviceRGB() );
240 genericRGBColorSpace
.reset( CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB
) );
244 return genericRGBColorSpace
;
247 #if wxOSX_USE_COCOA_OR_CARBON
249 CGColorRef
wxMacCreateCGColorFromHITheme( ThemeBrush brush
)
252 HIThemeBrushCreateCGColor( brush
, &color
);
256 //---------------------------------------------------------------------------
257 // Mac Specific string utility functions
258 //---------------------------------------------------------------------------
260 void wxMacStringToPascal( const wxString
&from
, unsigned char * to
)
262 wxCharBuffer buf
= from
.mb_str( wxConvLocal
);
263 int len
= strlen(buf
);
268 memcpy( (char*) &to
[1] , buf
, len
);
271 wxString
wxMacMakeStringFromPascal( const unsigned char * from
)
273 return wxString( (char*) &from
[1] , wxConvLocal
, from
[0] );
276 #endif // wxOSX_USE_COCOA_OR_CARBON