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
68 #if (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070) && (MAC_OS_X_VERSION_MIN_REQUIRED < 1060)
69 // bring back declaration so that we can support deployment targets < 10_6
70 CG_EXTERN
size_t CGDisplayBitsPerPixel(CGDirectDisplayID display
)
71 CG_AVAILABLE_BUT_DEPRECATED(__MAC_10_0
, __MAC_10_6
,
72 __IPHONE_NA
, __IPHONE_NA
);
75 #if wxOSX_USE_COCOA_OR_CARBON
76 // Returns depth of screen
81 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
82 if ( UMAGetSystemVersion() >= 0x1060 )
84 CGDisplayModeRef currentMode
= CGDisplayCopyDisplayMode(kCGDirectMainDisplay
);
85 CFStringRef encoding
= CGDisplayModeCopyPixelEncoding(currentMode
);
87 if(CFStringCompare(encoding
, CFSTR(IO32BitDirectPixels
), kCFCompareCaseInsensitive
) == kCFCompareEqualTo
)
89 else if(CFStringCompare(encoding
, CFSTR(IO16BitDirectPixels
), kCFCompareCaseInsensitive
) == kCFCompareEqualTo
)
91 else if(CFStringCompare(encoding
, CFSTR(IO8BitIndexedPixels
), kCFCompareCaseInsensitive
) == kCFCompareEqualTo
)
94 theDepth
= 32; // some reasonable default
101 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1060
102 theDepth
= (int) CGDisplayBitsPerPixel(CGMainDisplayID());
108 // Get size of display
109 void wxDisplaySize(int *width
, int *height
)
111 // TODO adapt for multi-displays
112 CGRect bounds
= CGDisplayBounds(CGMainDisplayID());
114 *width
= (int)bounds
.size
.width
;
116 *height
= (int)bounds
.size
.height
;
121 // ----------------------------------------------------------------------------
122 // Launch document with default app
123 // ----------------------------------------------------------------------------
125 bool wxLaunchDefaultApplication(const wxString
& document
, int flags
)
129 wxCFRef
<CFMutableStringRef
> cfMutableString(CFStringCreateMutableCopy(NULL
, 0, wxCFStringRef(document
)));
130 CFStringNormalize(cfMutableString
,kCFStringNormalizationFormD
);
131 wxCFRef
<CFURLRef
> curl(CFURLCreateWithFileSystemPath(kCFAllocatorDefault
, cfMutableString
, kCFURLPOSIXPathStyle
, false));
132 OSStatus err
= LSOpenCFURLRef( curl
, NULL
);
140 wxLogDebug(wxT("Default Application Launch error %d"), (int) err
);
145 // ----------------------------------------------------------------------------
146 // Launch default browser
147 // ----------------------------------------------------------------------------
149 bool wxDoLaunchDefaultBrowser(const wxString
& url
, int flags
)
152 wxCFRef
< CFURLRef
> curl( CFURLCreateWithString( kCFAllocatorDefault
,
153 wxCFStringRef( url
), NULL
) );
154 OSStatus err
= LSOpenCFURLRef( curl
, NULL
);
162 wxLogDebug(wxT("Browser Launch error %d"), (int) err
);
171 void wxDisplaySizeMM(int *width
, int *height
)
173 wxDisplaySize(width
, height
);
174 // on mac 72 is fixed (at least now;-)
175 double cvPt2Mm
= 25.4 / 72;
178 *width
= int( *width
* cvPt2Mm
);
181 *height
= int( *height
* cvPt2Mm
);
185 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
187 // We suppose that toolkit version is the same as OS version under Mac
188 wxGetOsVersion(verMaj
, verMin
);
193 wxEventLoopBase
* wxGUIAppTraits::CreateEventLoop()
195 return new wxEventLoop
;
198 wxWindow
* wxFindWindowAtPoint(wxWindow
* win
, const wxPoint
& pt
);
200 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
204 Point screenPoint
= { pt
.y
, pt
.x
};
207 if ( FindWindow( screenPoint
, &windowRef
) )
209 wxNonOwnedWindow
*nonOwned
= wxNonOwnedWindow::GetFromWXWindow( windowRef
);
212 return wxFindWindowAtPoint( nonOwned
, pt
);
219 return wxGenericFindWindowAtPoint( pt
);
225 Return the generic RGB color space. This is a 'get' function and the caller should
226 not release the returned value unless the caller retains it first. Usually callers
227 of this routine will immediately use the returned colorspace with CoreGraphics
228 so they typically do not need to retain it themselves.
230 This function creates the generic RGB color space once and hangs onto it so it can
231 return it whenever this function is called.
234 CGColorSpaceRef
wxMacGetGenericRGBColorSpace()
236 static wxCFRef
<CGColorSpaceRef
> genericRGBColorSpace
;
238 if (genericRGBColorSpace
== NULL
)
241 genericRGBColorSpace
.reset( CGColorSpaceCreateDeviceRGB() );
243 genericRGBColorSpace
.reset( CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB
) );
247 return genericRGBColorSpace
;
250 #if wxOSX_USE_COCOA_OR_CARBON
252 CGColorRef
wxMacCreateCGColorFromHITheme( ThemeBrush brush
)
255 HIThemeBrushCreateCGColor( brush
, &color
);
259 //---------------------------------------------------------------------------
260 // Mac Specific string utility functions
261 //---------------------------------------------------------------------------
263 void wxMacStringToPascal( const wxString
&from
, unsigned char * to
)
265 wxCharBuffer buf
= from
.mb_str( wxConvLocal
);
266 int len
= strlen(buf
);
271 memcpy( (char*) &to
[1] , buf
, len
);
274 wxString
wxMacMakeStringFromPascal( const unsigned char * from
)
276 return wxString( (char*) &from
[1] , wxConvLocal
, from
[0] );
279 #endif // wxOSX_USE_COCOA_OR_CARBON