1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/utils_osx.cpp
3 // Purpose: Various utilities
4 // Author: Stefan Csomor
7 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
21 #include "wx/toplevel.h"
26 #include "wx/apptrait.h"
35 // #include "MoreFilesX.h"
37 #include <AudioToolbox/AudioServices.h>
39 #include "wx/osx/private.h"
40 #include "wx/osx/private/timer.h"
42 #include "wx/evtloop.h"
44 // Check whether this window wants to process messages, e.g. Stop button
45 // in long calculations.
46 bool wxCheckForInterrupt(wxWindow
*WXUNUSED(wnd
))
52 // Return true if we have a colour display
53 bool wxColourDisplay()
55 // always the case on OS X
60 #if wxOSX_USE_COCOA_OR_CARBON
62 #if (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070) && (MAC_OS_X_VERSION_MIN_REQUIRED < 1060)
63 // bring back declaration so that we can support deployment targets < 10_6
64 CG_EXTERN
size_t CGDisplayBitsPerPixel(CGDirectDisplayID display
)
65 CG_AVAILABLE_BUT_DEPRECATED(__MAC_10_0
, __MAC_10_6
,
66 __IPHONE_NA
, __IPHONE_NA
);
69 // Returns depth of screen
74 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
75 if ( UMAGetSystemVersion() >= 0x1060 )
77 CGDisplayModeRef currentMode
= CGDisplayCopyDisplayMode(kCGDirectMainDisplay
);
78 CFStringRef encoding
= CGDisplayModeCopyPixelEncoding(currentMode
);
80 if(CFStringCompare(encoding
, CFSTR(IO32BitDirectPixels
), kCFCompareCaseInsensitive
) == kCFCompareEqualTo
)
82 else if(CFStringCompare(encoding
, CFSTR(IO16BitDirectPixels
), kCFCompareCaseInsensitive
) == kCFCompareEqualTo
)
84 else if(CFStringCompare(encoding
, CFSTR(IO8BitIndexedPixels
), kCFCompareCaseInsensitive
) == kCFCompareEqualTo
)
87 theDepth
= 32; // some reasonable default
90 CGDisplayModeRelease(currentMode
);
95 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1060
96 theDepth
= (int) CGDisplayBitsPerPixel(CGMainDisplayID());
102 // Get size of display
103 void wxDisplaySize(int *width
, int *height
)
105 // TODO adapt for multi-displays
106 CGRect bounds
= CGDisplayBounds(CGMainDisplayID());
108 *width
= (int)bounds
.size
.width
;
110 *height
= (int)bounds
.size
.height
;
115 // ----------------------------------------------------------------------------
116 // Launch document with default app
117 // ----------------------------------------------------------------------------
119 bool wxLaunchDefaultApplication(const wxString
& document
, int flags
)
123 wxCFRef
<CFMutableStringRef
> cfMutableString(CFStringCreateMutableCopy(NULL
, 0, wxCFStringRef(document
)));
124 CFStringNormalize(cfMutableString
,kCFStringNormalizationFormD
);
125 wxCFRef
<CFURLRef
> curl(CFURLCreateWithFileSystemPath(kCFAllocatorDefault
, cfMutableString
, kCFURLPOSIXPathStyle
, false));
126 OSStatus err
= LSOpenCFURLRef( curl
, NULL
);
134 wxLogDebug(wxT("Default Application Launch error %d"), (int) err
);
139 // ----------------------------------------------------------------------------
140 // Launch default browser
141 // ----------------------------------------------------------------------------
143 bool wxDoLaunchDefaultBrowser(const wxString
& url
, int flags
)
146 wxCFRef
< CFURLRef
> curl( CFURLCreateWithString( kCFAllocatorDefault
,
147 wxCFStringRef( url
), NULL
) );
148 OSStatus err
= LSOpenCFURLRef( curl
, NULL
);
156 wxLogDebug(wxT("Browser Launch error %d"), (int) err
);
165 void wxDisplaySizeMM(int *width
, int *height
)
167 wxDisplaySize(width
, height
);
168 // on mac 72 is fixed (at least now;-)
169 double cvPt2Mm
= 25.4 / 72;
172 *width
= int( *width
* cvPt2Mm
);
175 *height
= int( *height
* cvPt2Mm
);
179 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
181 // We suppose that toolkit version is the same as OS version under Mac
182 wxGetOsVersion(verMaj
, verMin
);
187 wxEventLoopBase
* wxGUIAppTraits::CreateEventLoop()
189 return new wxEventLoop
;
192 wxWindow
* wxFindWindowAtPoint(wxWindow
* win
, const wxPoint
& pt
);
194 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
198 Point screenPoint
= { pt
.y
, pt
.x
};
201 if ( FindWindow( screenPoint
, &windowRef
) )
203 wxNonOwnedWindow
*nonOwned
= wxNonOwnedWindow::GetFromWXWindow( windowRef
);
206 return wxFindWindowAtPoint( nonOwned
, pt
);
213 return wxGenericFindWindowAtPoint( pt
);
219 Return the generic RGB color space. This is a 'get' function and the caller should
220 not release the returned value unless the caller retains it first. Usually callers
221 of this routine will immediately use the returned colorspace with CoreGraphics
222 so they typically do not need to retain it themselves.
224 This function creates the generic RGB color space once and hangs onto it so it can
225 return it whenever this function is called.
228 CGColorSpaceRef
wxMacGetGenericRGBColorSpace()
230 static wxCFRef
<CGColorSpaceRef
> genericRGBColorSpace
;
232 if (genericRGBColorSpace
== NULL
)
235 genericRGBColorSpace
.reset( CGColorSpaceCreateDeviceRGB() );
237 genericRGBColorSpace
.reset( CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB
) );
241 return genericRGBColorSpace
;
244 #if wxOSX_USE_COCOA_OR_CARBON
246 CGColorRef
wxMacCreateCGColorFromHITheme( ThemeBrush brush
)
248 const int maxcachedbrush
= 58+5; // negative indices are for metabrushes, cache down to -5)
249 int brushindex
= brush
+5;
250 if ( brushindex
< 0 || brushindex
> maxcachedbrush
)
253 HIThemeBrushCreateCGColor( brush
, &color
);
258 static bool inited
= false;
259 static CGColorRef themecolors
[maxcachedbrush
+1];
262 for ( int i
= 0 ; i
<= maxcachedbrush
; ++i
)
263 HIThemeBrushCreateCGColor( i
-5, &themecolors
[i
] );
266 return CGColorRetain(themecolors
[brushindex
]);
270 //---------------------------------------------------------------------------
271 // Mac Specific string utility functions
272 //---------------------------------------------------------------------------
274 void wxMacStringToPascal( const wxString
&from
, unsigned char * to
)
276 wxCharBuffer buf
= from
.mb_str( wxConvLocal
);
277 int len
= strlen(buf
);
282 memcpy( (char*) &to
[1] , buf
, len
);
285 wxString
wxMacMakeStringFromPascal( const unsigned char * from
)
287 return wxString( (char*) &from
[1] , wxConvLocal
, from
[0] );
290 #endif // wxOSX_USE_COCOA_OR_CARBON