]> git.saurik.com Git - wxWidgets.git/blob - src/osx/utils_osx.cpp
Correct format specifiers used to show wxIPV4address.
[wxWidgets.git] / src / osx / utils_osx.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/utils_osx.cpp
3 // Purpose: Various utilities
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id: utils.cpp 54886 2008-07-31 13:02:53Z SC $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12
13 #include "wx/wxprec.h"
14
15 #include "wx/utils.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/intl.h"
19 #include "wx/app.h"
20 #include "wx/log.h"
21 #if wxUSE_GUI
22 #include "wx/toplevel.h"
23 #include "wx/font.h"
24 #endif
25 #endif
26
27 #include "wx/apptrait.h"
28
29 #include <ctype.h>
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <stdarg.h>
35
36 // #include "MoreFilesX.h"
37
38 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
39 #include <AudioToolbox/AudioServices.h>
40 #endif
41
42 #include "wx/osx/private.h"
43 #include "wx/osx/private/timer.h"
44
45 #include "wx/evtloop.h"
46
47 #if defined(__MWERKS__) && wxUSE_UNICODE
48 #if __MWERKS__ < 0x4100
49 #include <wtime.h>
50 #endif
51 #endif
52
53 // Check whether this window wants to process messages, e.g. Stop button
54 // in long calculations.
55 bool wxCheckForInterrupt(wxWindow *WXUNUSED(wnd))
56 {
57 // TODO
58 return false;
59 }
60
61 // Return true if we have a colour display
62 bool wxColourDisplay()
63 {
64 return true;
65 }
66
67 #if wxOSX_USE_COCOA_OR_CARBON
68 // Returns depth of screen
69 int wxDisplayDepth()
70 {
71 int theDepth = (int) CGDisplayBitsPerPixel(CGMainDisplayID());
72 return theDepth;
73 }
74
75 // Get size of display
76 void wxDisplaySize(int *width, int *height)
77 {
78 // TODO adapt for multi-displays
79 CGRect bounds = CGDisplayBounds(CGMainDisplayID());
80 if ( width )
81 *width = (int)bounds.size.width ;
82 if ( height )
83 *height = (int)bounds.size.height;
84 }
85
86 #if wxUSE_GUI
87
88 // ----------------------------------------------------------------------------
89 // Launch document with default app
90 // ----------------------------------------------------------------------------
91
92 bool wxLaunchDefaultApplication(const wxString& document, int flags)
93 {
94 wxUnusedVar(flags);
95
96 wxCFRef<CFMutableStringRef> cfMutableString(CFStringCreateMutableCopy(NULL, 0, wxCFStringRef(document)));
97 CFStringNormalize(cfMutableString,kCFStringNormalizationFormD);
98 wxCFRef<CFURLRef> curl(CFURLCreateWithFileSystemPath(kCFAllocatorDefault, cfMutableString , kCFURLPOSIXPathStyle, false));
99 OSStatus err = LSOpenCFURLRef( curl , NULL );
100
101 if (err == noErr)
102 {
103 return true;
104 }
105 else
106 {
107 wxLogDebug(wxT("Default Application Launch error %d"), (int) err);
108 return false;
109 }
110 }
111
112 // ----------------------------------------------------------------------------
113 // Launch default browser
114 // ----------------------------------------------------------------------------
115
116 bool wxDoLaunchDefaultBrowser(const wxString& url, int flags)
117 {
118 wxUnusedVar(flags);
119 wxCFRef< CFURLRef > curl( CFURLCreateWithString( kCFAllocatorDefault,
120 wxCFStringRef( url ), NULL ) );
121 OSStatus err = LSOpenCFURLRef( curl , NULL );
122
123 if (err == noErr)
124 {
125 return true;
126 }
127 else
128 {
129 wxLogDebug(wxT("Browser Launch error %d"), (int) err);
130 return false;
131 }
132 }
133
134 #endif // wxUSE_GUI
135
136 #endif
137
138 void wxDisplaySizeMM(int *width, int *height)
139 {
140 wxDisplaySize(width, height);
141 // on mac 72 is fixed (at least now;-)
142 double cvPt2Mm = 25.4 / 72;
143
144 if (width != NULL)
145 *width = int( *width * cvPt2Mm );
146
147 if (height != NULL)
148 *height = int( *height * cvPt2Mm );
149 }
150
151
152 wxPortId wxGUIAppTraits::GetToolkitVersion(int *verMaj, int *verMin) const
153 {
154 // We suppose that toolkit version is the same as OS version under Mac
155 wxGetOsVersion(verMaj, verMin);
156
157 return wxPORT_OSX;
158 }
159
160 wxEventLoopBase* wxGUIAppTraits::CreateEventLoop()
161 {
162 return new wxEventLoop;
163 }
164
165 wxWindow* wxFindWindowAtPoint(wxWindow* win, const wxPoint& pt);
166
167 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
168 {
169 #if wxOSX_USE_CARBON
170
171 Point screenPoint = { pt.y , pt.x };
172 WindowRef windowRef;
173
174 if ( FindWindow( screenPoint , &windowRef ) )
175 {
176 wxNonOwnedWindow *nonOwned = wxNonOwnedWindow::GetFromWXWindow( windowRef );
177
178 if ( nonOwned )
179 return wxFindWindowAtPoint( nonOwned , pt );
180 }
181
182 return NULL;
183
184 #else
185
186 return wxGenericFindWindowAtPoint( pt );
187
188 #endif
189 }
190
191 /*
192 Return the generic RGB color space. This is a 'get' function and the caller should
193 not release the returned value unless the caller retains it first. Usually callers
194 of this routine will immediately use the returned colorspace with CoreGraphics
195 so they typically do not need to retain it themselves.
196
197 This function creates the generic RGB color space once and hangs onto it so it can
198 return it whenever this function is called.
199 */
200
201 CGColorSpaceRef wxMacGetGenericRGBColorSpace()
202 {
203 static wxCFRef<CGColorSpaceRef> genericRGBColorSpace;
204
205 if (genericRGBColorSpace == NULL)
206 {
207 #if wxOSX_USE_IPHONE
208 genericRGBColorSpace.reset( CGColorSpaceCreateDeviceRGB() );
209 #else
210 genericRGBColorSpace.reset( CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB ) );
211 #endif
212 }
213
214 return genericRGBColorSpace;
215 }
216
217 #if wxOSX_USE_COCOA_OR_CARBON
218
219 CGColorRef wxMacCreateCGColorFromHITheme( ThemeBrush brush )
220 {
221 CGColorRef color ;
222 HIThemeBrushCreateCGColor( brush, &color );
223 return color;
224 }
225
226 //---------------------------------------------------------------------------
227 // Mac Specific string utility functions
228 //---------------------------------------------------------------------------
229
230 void wxMacStringToPascal( const wxString&from , unsigned char * to )
231 {
232 wxCharBuffer buf = from.mb_str( wxConvLocal );
233 int len = strlen(buf);
234
235 if ( len > 255 )
236 len = 255;
237 to[0] = len;
238 memcpy( (char*) &to[1] , buf , len );
239 }
240
241 wxString wxMacMakeStringFromPascal( const unsigned char * from )
242 {
243 return wxString( (char*) &from[1] , wxConvLocal , from[0] );
244 }
245
246 #endif // wxOSX_USE_COCOA_OR_CARBON