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 #include <AudioToolbox/AudioServices.h>
40 #include "wx/osx/private.h"
41 #include "wx/osx/private/timer.h"
43 #include "wx/evtloop.h"
45 // Check whether this window wants to process messages, e.g. Stop button
46 // in long calculations.
47 bool wxCheckForInterrupt(wxWindow
*WXUNUSED(wnd
))
53 // Return true if we have a colour display
54 bool wxColourDisplay()
56 // always the case on OS X
61 #if wxOSX_USE_COCOA_OR_CARBON
63 #if (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070) && (MAC_OS_X_VERSION_MIN_REQUIRED < 1060)
64 // bring back declaration so that we can support deployment targets < 10_6
65 CG_EXTERN
size_t CGDisplayBitsPerPixel(CGDirectDisplayID display
)
66 CG_AVAILABLE_BUT_DEPRECATED(__MAC_10_0
, __MAC_10_6
,
67 __IPHONE_NA
, __IPHONE_NA
);
70 // Returns depth of screen
75 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
76 if ( UMAGetSystemVersion() >= 0x1060 )
78 CGDisplayModeRef currentMode
= CGDisplayCopyDisplayMode(kCGDirectMainDisplay
);
79 CFStringRef encoding
= CGDisplayModeCopyPixelEncoding(currentMode
);
81 if(CFStringCompare(encoding
, CFSTR(IO32BitDirectPixels
), kCFCompareCaseInsensitive
) == kCFCompareEqualTo
)
83 else if(CFStringCompare(encoding
, CFSTR(IO16BitDirectPixels
), kCFCompareCaseInsensitive
) == kCFCompareEqualTo
)
85 else if(CFStringCompare(encoding
, CFSTR(IO8BitIndexedPixels
), kCFCompareCaseInsensitive
) == kCFCompareEqualTo
)
88 theDepth
= 32; // some reasonable default
91 CGDisplayModeRelease(currentMode
);
96 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1060
97 theDepth
= (int) CGDisplayBitsPerPixel(CGMainDisplayID());
103 // Get size of display
104 void wxDisplaySize(int *width
, int *height
)
106 // TODO adapt for multi-displays
107 CGRect bounds
= CGDisplayBounds(CGMainDisplayID());
109 *width
= (int)bounds
.size
.width
;
111 *height
= (int)bounds
.size
.height
;
116 // ----------------------------------------------------------------------------
117 // Launch document with default app
118 // ----------------------------------------------------------------------------
120 bool wxLaunchDefaultApplication(const wxString
& document
, int flags
)
124 wxCFRef
<CFMutableStringRef
> cfMutableString(CFStringCreateMutableCopy(NULL
, 0, wxCFStringRef(document
)));
125 CFStringNormalize(cfMutableString
,kCFStringNormalizationFormD
);
126 wxCFRef
<CFURLRef
> curl(CFURLCreateWithFileSystemPath(kCFAllocatorDefault
, cfMutableString
, kCFURLPOSIXPathStyle
, false));
127 OSStatus err
= LSOpenCFURLRef( curl
, NULL
);
135 wxLogDebug(wxT("Default Application Launch error %d"), (int) err
);
140 // ----------------------------------------------------------------------------
141 // Launch default browser
142 // ----------------------------------------------------------------------------
144 bool wxDoLaunchDefaultBrowser(const wxString
& url
, int flags
)
147 wxCFRef
< CFURLRef
> curl( CFURLCreateWithString( kCFAllocatorDefault
,
148 wxCFStringRef( url
), NULL
) );
149 OSStatus err
= LSOpenCFURLRef( curl
, NULL
);
157 wxLogDebug(wxT("Browser Launch error %d"), (int) err
);
166 void wxDisplaySizeMM(int *width
, int *height
)
168 wxDisplaySize(width
, height
);
169 // on mac 72 is fixed (at least now;-)
170 double cvPt2Mm
= 25.4 / 72;
173 *width
= int( *width
* cvPt2Mm
);
176 *height
= int( *height
* cvPt2Mm
);
180 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
182 // We suppose that toolkit version is the same as OS version under Mac
183 wxGetOsVersion(verMaj
, verMin
);
188 wxEventLoopBase
* wxGUIAppTraits::CreateEventLoop()
190 return new wxEventLoop
;
193 wxWindow
* wxFindWindowAtPoint(wxWindow
* win
, const wxPoint
& pt
);
195 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
199 Point screenPoint
= { pt
.y
, pt
.x
};
202 if ( FindWindow( screenPoint
, &windowRef
) )
204 wxNonOwnedWindow
*nonOwned
= wxNonOwnedWindow::GetFromWXWindow( windowRef
);
207 return wxFindWindowAtPoint( nonOwned
, pt
);
214 return wxGenericFindWindowAtPoint( pt
);
220 Return the generic RGB color space. This is a 'get' function and the caller should
221 not release the returned value unless the caller retains it first. Usually callers
222 of this routine will immediately use the returned colorspace with CoreGraphics
223 so they typically do not need to retain it themselves.
225 This function creates the generic RGB color space once and hangs onto it so it can
226 return it whenever this function is called.
229 CGColorSpaceRef
wxMacGetGenericRGBColorSpace()
231 static wxCFRef
<CGColorSpaceRef
> genericRGBColorSpace
;
233 if (genericRGBColorSpace
== NULL
)
236 genericRGBColorSpace
.reset( CGColorSpaceCreateDeviceRGB() );
238 genericRGBColorSpace
.reset( CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB
) );
242 return genericRGBColorSpace
;
245 #if wxOSX_USE_COCOA_OR_CARBON
247 CGColorRef
wxMacCreateCGColorFromHITheme( ThemeBrush brush
)
249 const int maxcachedbrush
= 58+5; // negative indices are for metabrushes, cache down to -5)
250 int brushindex
= brush
+5;
251 if ( brushindex
< 0 || brush
> brushindex
)
254 HIThemeBrushCreateCGColor( brush
, &color
);
259 static bool inited
= false;
260 static CGColorRef themecolors
[maxcachedbrush
+1];
263 for ( int i
= 0 ; i
<= maxcachedbrush
; ++i
)
264 HIThemeBrushCreateCGColor( i
-5, &themecolors
[i
] );
267 return CGColorRetain(themecolors
[brushindex
]);
271 //---------------------------------------------------------------------------
272 // Mac Specific string utility functions
273 //---------------------------------------------------------------------------
275 void wxMacStringToPascal( const wxString
&from
, unsigned char * to
)
277 wxCharBuffer buf
= from
.mb_str( wxConvLocal
);
278 int len
= strlen(buf
);
283 memcpy( (char*) &to
[1] , buf
, len
);
286 wxString
wxMacMakeStringFromPascal( const unsigned char * from
)
288 return wxString( (char*) &from
[1] , wxConvLocal
, from
[0] );
291 #endif // wxOSX_USE_COCOA_OR_CARBON