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 #if defined(__MWERKS__) && wxUSE_UNICODE
48 #if __MWERKS__ < 0x4100
53 // Check whether this window wants to process messages, e.g. Stop button
54 // in long calculations.
55 bool wxCheckForInterrupt(wxWindow
*WXUNUSED(wnd
))
61 // Return true if we have a colour display
62 bool wxColourDisplay()
64 // always the case on OS X
69 #if wxOSX_USE_COCOA_OR_CARBON
71 #if (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070) && (MAC_OS_X_VERSION_MIN_REQUIRED < 1060)
72 // bring back declaration so that we can support deployment targets < 10_6
73 CG_EXTERN
size_t CGDisplayBitsPerPixel(CGDirectDisplayID display
)
74 CG_AVAILABLE_BUT_DEPRECATED(__MAC_10_0
, __MAC_10_6
,
75 __IPHONE_NA
, __IPHONE_NA
);
78 // Returns depth of screen
83 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
84 if ( UMAGetSystemVersion() >= 0x1060 )
86 CGDisplayModeRef currentMode
= CGDisplayCopyDisplayMode(kCGDirectMainDisplay
);
87 CFStringRef encoding
= CGDisplayModeCopyPixelEncoding(currentMode
);
89 if(CFStringCompare(encoding
, CFSTR(IO32BitDirectPixels
), kCFCompareCaseInsensitive
) == kCFCompareEqualTo
)
91 else if(CFStringCompare(encoding
, CFSTR(IO16BitDirectPixels
), kCFCompareCaseInsensitive
) == kCFCompareEqualTo
)
93 else if(CFStringCompare(encoding
, CFSTR(IO8BitIndexedPixels
), kCFCompareCaseInsensitive
) == kCFCompareEqualTo
)
96 theDepth
= 32; // some reasonable default
103 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1060
104 theDepth
= (int) CGDisplayBitsPerPixel(CGMainDisplayID());
110 // Get size of display
111 void wxDisplaySize(int *width
, int *height
)
113 // TODO adapt for multi-displays
114 CGRect bounds
= CGDisplayBounds(CGMainDisplayID());
116 *width
= (int)bounds
.size
.width
;
118 *height
= (int)bounds
.size
.height
;
123 // ----------------------------------------------------------------------------
124 // Launch document with default app
125 // ----------------------------------------------------------------------------
127 bool wxLaunchDefaultApplication(const wxString
& document
, int flags
)
131 wxCFRef
<CFMutableStringRef
> cfMutableString(CFStringCreateMutableCopy(NULL
, 0, wxCFStringRef(document
)));
132 CFStringNormalize(cfMutableString
,kCFStringNormalizationFormD
);
133 wxCFRef
<CFURLRef
> curl(CFURLCreateWithFileSystemPath(kCFAllocatorDefault
, cfMutableString
, kCFURLPOSIXPathStyle
, false));
134 OSStatus err
= LSOpenCFURLRef( curl
, NULL
);
142 wxLogDebug(wxT("Default Application Launch error %d"), (int) err
);
147 // ----------------------------------------------------------------------------
148 // Launch default browser
149 // ----------------------------------------------------------------------------
151 bool wxDoLaunchDefaultBrowser(const wxString
& url
, int flags
)
154 wxCFRef
< CFURLRef
> curl( CFURLCreateWithString( kCFAllocatorDefault
,
155 wxCFStringRef( url
), NULL
) );
156 OSStatus err
= LSOpenCFURLRef( curl
, NULL
);
164 wxLogDebug(wxT("Browser Launch error %d"), (int) err
);
173 void wxDisplaySizeMM(int *width
, int *height
)
175 wxDisplaySize(width
, height
);
176 // on mac 72 is fixed (at least now;-)
177 double cvPt2Mm
= 25.4 / 72;
180 *width
= int( *width
* cvPt2Mm
);
183 *height
= int( *height
* cvPt2Mm
);
187 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
189 // We suppose that toolkit version is the same as OS version under Mac
190 wxGetOsVersion(verMaj
, verMin
);
195 wxEventLoopBase
* wxGUIAppTraits::CreateEventLoop()
197 return new wxEventLoop
;
200 wxWindow
* wxFindWindowAtPoint(wxWindow
* win
, const wxPoint
& pt
);
202 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
206 Point screenPoint
= { pt
.y
, pt
.x
};
209 if ( FindWindow( screenPoint
, &windowRef
) )
211 wxNonOwnedWindow
*nonOwned
= wxNonOwnedWindow::GetFromWXWindow( windowRef
);
214 return wxFindWindowAtPoint( nonOwned
, pt
);
221 return wxGenericFindWindowAtPoint( pt
);
227 Return the generic RGB color space. This is a 'get' function and the caller should
228 not release the returned value unless the caller retains it first. Usually callers
229 of this routine will immediately use the returned colorspace with CoreGraphics
230 so they typically do not need to retain it themselves.
232 This function creates the generic RGB color space once and hangs onto it so it can
233 return it whenever this function is called.
236 CGColorSpaceRef
wxMacGetGenericRGBColorSpace()
238 static wxCFRef
<CGColorSpaceRef
> genericRGBColorSpace
;
240 if (genericRGBColorSpace
== NULL
)
243 genericRGBColorSpace
.reset( CGColorSpaceCreateDeviceRGB() );
245 genericRGBColorSpace
.reset( CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB
) );
249 return genericRGBColorSpace
;
252 #if wxOSX_USE_COCOA_OR_CARBON
254 CGColorRef
wxMacCreateCGColorFromHITheme( ThemeBrush brush
)
257 HIThemeBrushCreateCGColor( brush
, &color
);
261 //---------------------------------------------------------------------------
262 // Mac Specific string utility functions
263 //---------------------------------------------------------------------------
265 void wxMacStringToPascal( const wxString
&from
, unsigned char * to
)
267 wxCharBuffer buf
= from
.mb_str( wxConvLocal
);
268 int len
= strlen(buf
);
273 memcpy( (char*) &to
[1] , buf
, len
);
276 wxString
wxMacMakeStringFromPascal( const unsigned char * from
)
278 return wxString( (char*) &from
[1] , wxConvLocal
, from
[0] );
281 #endif // wxOSX_USE_COCOA_OR_CARBON