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
97 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1060
98 theDepth
= (int) CGDisplayBitsPerPixel(CGMainDisplayID());
104 // Get size of display
105 void wxDisplaySize(int *width
, int *height
)
107 // TODO adapt for multi-displays
108 CGRect bounds
= CGDisplayBounds(CGMainDisplayID());
110 *width
= (int)bounds
.size
.width
;
112 *height
= (int)bounds
.size
.height
;
117 // ----------------------------------------------------------------------------
118 // Launch document with default app
119 // ----------------------------------------------------------------------------
121 bool wxLaunchDefaultApplication(const wxString
& document
, int flags
)
125 wxCFRef
<CFMutableStringRef
> cfMutableString(CFStringCreateMutableCopy(NULL
, 0, wxCFStringRef(document
)));
126 CFStringNormalize(cfMutableString
,kCFStringNormalizationFormD
);
127 wxCFRef
<CFURLRef
> curl(CFURLCreateWithFileSystemPath(kCFAllocatorDefault
, cfMutableString
, kCFURLPOSIXPathStyle
, false));
128 OSStatus err
= LSOpenCFURLRef( curl
, NULL
);
136 wxLogDebug(wxT("Default Application Launch error %d"), (int) err
);
141 // ----------------------------------------------------------------------------
142 // Launch default browser
143 // ----------------------------------------------------------------------------
145 bool wxDoLaunchDefaultBrowser(const wxString
& url
, int flags
)
148 wxCFRef
< CFURLRef
> curl( CFURLCreateWithString( kCFAllocatorDefault
,
149 wxCFStringRef( url
), NULL
) );
150 OSStatus err
= LSOpenCFURLRef( curl
, NULL
);
158 wxLogDebug(wxT("Browser Launch error %d"), (int) err
);
167 void wxDisplaySizeMM(int *width
, int *height
)
169 wxDisplaySize(width
, height
);
170 // on mac 72 is fixed (at least now;-)
171 double cvPt2Mm
= 25.4 / 72;
174 *width
= int( *width
* cvPt2Mm
);
177 *height
= int( *height
* cvPt2Mm
);
181 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
183 // We suppose that toolkit version is the same as OS version under Mac
184 wxGetOsVersion(verMaj
, verMin
);
189 wxEventLoopBase
* wxGUIAppTraits::CreateEventLoop()
191 return new wxEventLoop
;
194 wxWindow
* wxFindWindowAtPoint(wxWindow
* win
, const wxPoint
& pt
);
196 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
200 Point screenPoint
= { pt
.y
, pt
.x
};
203 if ( FindWindow( screenPoint
, &windowRef
) )
205 wxNonOwnedWindow
*nonOwned
= wxNonOwnedWindow::GetFromWXWindow( windowRef
);
208 return wxFindWindowAtPoint( nonOwned
, pt
);
215 return wxGenericFindWindowAtPoint( pt
);
221 Return the generic RGB color space. This is a 'get' function and the caller should
222 not release the returned value unless the caller retains it first. Usually callers
223 of this routine will immediately use the returned colorspace with CoreGraphics
224 so they typically do not need to retain it themselves.
226 This function creates the generic RGB color space once and hangs onto it so it can
227 return it whenever this function is called.
230 CGColorSpaceRef
wxMacGetGenericRGBColorSpace()
232 static wxCFRef
<CGColorSpaceRef
> genericRGBColorSpace
;
234 if (genericRGBColorSpace
== NULL
)
237 genericRGBColorSpace
.reset( CGColorSpaceCreateDeviceRGB() );
239 genericRGBColorSpace
.reset( CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB
) );
243 return genericRGBColorSpace
;
246 #if wxOSX_USE_COCOA_OR_CARBON
248 CGColorRef
wxMacCreateCGColorFromHITheme( ThemeBrush brush
)
251 HIThemeBrushCreateCGColor( brush
, &color
);
255 //---------------------------------------------------------------------------
256 // Mac Specific string utility functions
257 //---------------------------------------------------------------------------
259 void wxMacStringToPascal( const wxString
&from
, unsigned char * to
)
261 wxCharBuffer buf
= from
.mb_str( wxConvLocal
);
262 int len
= strlen(buf
);
267 memcpy( (char*) &to
[1] , buf
, len
);
270 wxString
wxMacMakeStringFromPascal( const unsigned char * from
)
272 return wxString( (char*) &from
[1] , wxConvLocal
, from
[0] );
275 #endif // wxOSX_USE_COCOA_OR_CARBON