]> git.saurik.com Git - wxWidgets.git/blob - include/wx/wxcrtvararg.h
added wxBG_STYLE_TRANSPARENT and implemented it for wxMac; documented various transpa...
[wxWidgets.git] / include / wx / wxcrtvararg.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/wxcrtvararg.h
3 // Purpose: Type-safe ANSI and Unicode builds compatible wrappers for
4 // printf(), scanf() and related CRT functions
5 // Author: Joel Farley, Ove Kåven
6 // Modified by: Vadim Zeitlin, Robert Roebling, Ron Lee
7 // Created: 2007-02-19
8 // RCS-ID: $Id$
9 // Copyright: (c) 2007 REA Elektronik GmbH
10 // Licence: wxWindows licence
11 ///////////////////////////////////////////////////////////////////////////////
12
13 #ifndef _WX_WXCRTVARARG_H_
14 #define _WX_WXCRTVARARG_H_
15
16 // NB: User code should include wx/crt.h instead of including this
17 // header directly.
18
19 #include "wx/wxcrt.h"
20 #include "wx/strvararg.h"
21
22 #include "wx/string.h"
23
24 // ----------------------------------------------------------------------------
25 // CRT functions aliases
26 // ----------------------------------------------------------------------------
27
28 /* Required for wxPrintf() etc */
29 #include <stdarg.h>
30
31 /* printf() family saga */
32
33 /*
34 For some systems [v]snprintf() exists in the system libraries but not in the
35 headers, so we need to declare it ourselves to be able to use it.
36 */
37 #if defined(HAVE_VSNPRINTF) && !defined(HAVE_VSNPRINTF_DECL)
38 #ifdef __cplusplus
39 extern "C"
40 #else
41 extern
42 #endif
43 int vsnprintf(char *str, size_t size, const char *format, va_list ap);
44 #endif /* !HAVE_VSNPRINTF_DECL */
45
46 #if defined(HAVE_SNPRINTF) && !defined(HAVE_SNPRINTF_DECL)
47 #ifdef __cplusplus
48 extern "C"
49 #else
50 extern
51 #endif
52 int snprintf(char *str, size_t size, const char *format, ...);
53 #endif /* !HAVE_SNPRINTF_DECL */
54
55 /* Wrapper for vsnprintf if it's 3rd parameter is non-const. Note: the
56 * same isn't done for snprintf below, the builtin wxSnprintf_ is used
57 * instead since it's already a simple wrapper */
58 #if defined __cplusplus && defined HAVE_BROKEN_VSNPRINTF_DECL
59 inline int wx_fixed_vsnprintf(char *str, size_t size, const char *format, va_list ap)
60 {
61 return vsnprintf(str, size, (char*)format, ap);
62 }
63 #endif
64
65 /*
66 MinGW MSVCRT has non-standard vswprintf() (for MSVC compatibility
67 presumably) and normally _vsnwprintf() is used instead
68 */
69 #if defined(HAVE_VSWPRINTF) && defined(__MINGW32__)
70 #undef HAVE_VSWPRINTF
71 #endif
72
73 #if defined(__WATCOMC__)
74 #define HAVE_VSWPRINTF 1
75 #endif
76
77 #if wxUSE_PRINTF_POS_PARAMS
78 /*
79 The systems where vsnprintf() supports positional parameters should
80 define the HAVE_UNIX98_PRINTF symbol.
81
82 On systems which don't (e.g. Windows) we are forced to use
83 our wxVsnprintf() implementation.
84 */
85 #if defined(HAVE_UNIX98_PRINTF)
86 #ifdef HAVE_VSWPRINTF
87 #define wxCRT_VsnprintfW vswprintf
88 #endif
89 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
90 #define wxCRT_VsnprintfA wx_fixed_vsnprintf
91 #else
92 #define wxCRT_VsnprintfA vsnprintf
93 #endif
94 #else /* !HAVE_UNIX98_PRINTF */
95 /*
96 The only compiler with positional parameters support under Windows
97 is VC++ 8.0 which provides a new xxprintf_p() functions family.
98 The 2003 PSDK includes a slightly earlier version of VC8 than the
99 main release and does not have the printf_p functions.
100 */
101 #if defined _MSC_FULL_VER && _MSC_FULL_VER >= 140050727 && !defined __WXWINCE__
102 #define wxCRT_VsnprintfA _vsprintf_p
103 #define wxCRT_VsnprintfW _vswprintf_p
104 #endif
105 #endif /* HAVE_UNIX98_PRINTF/!HAVE_UNIX98_PRINTF */
106 #else /* !wxUSE_PRINTF_POS_PARAMS */
107 /*
108 We always want to define safe snprintf() function to be used instead of
109 sprintf(). Some compilers already have it (or rather vsnprintf() which
110 we really need...), otherwise we implement it using our own printf()
111 code.
112
113 We define function with a trailing underscore here because the real one
114 is a wrapper around it as explained below
115 */
116
117 #if defined(__VISUALC__) || \
118 (defined(__BORLANDC__) && __BORLANDC__ >= 0x540)
119 #define wxCRT_VsnprintfA _vsnprintf
120 #define wxCRT_VsnprintfW _vsnwprintf
121 #else
122 #if defined(HAVE__VSNWPRINTF)
123 #define wxCRT_VsnprintfW _vsnwprintf
124 #elif defined(HAVE_VSWPRINTF)
125 #define wxCRT_VsnprintfW vswprintf
126 #elif defined(__WATCOMC__)
127 #define wxCRT_VsnprintfW _vsnwprintf
128 #endif
129
130 /*
131 All versions of CodeWarrior supported by wxWidgets apparently
132 have both snprintf() and vsnprintf()
133 */
134 #if defined(HAVE_VSNPRINTF) \
135 || defined(__MWERKS__) || defined(__WATCOMC__)
136 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
137 #define wxCRT_VsnprintfA wx_fixed_vsnprintf
138 #else
139 #define wxCRT_VsnprintfA vsnprintf
140 #endif
141 #endif
142 #endif
143 #endif /* wxUSE_PRINTF_POS_PARAMS/!wxUSE_PRINTF_POS_PARAMS */
144
145 #ifndef wxCRT_VsnprintfW
146 /* no (suitable) vsnprintf(), cook our own */
147 WXDLLIMPEXP_BASE int
148 wxCRT_VsnprintfW(wchar_t *buf, size_t len, const wchar_t *format, va_list argptr);
149 #define wxUSE_WXVSNPRINTFW 1
150 #else
151 #define wxUSE_WXVSNPRINTFW 0
152 #endif
153
154 #ifndef wxCRT_VsnprintfA
155 /* no (suitable) vsnprintf(), cook our own */
156 WXDLLIMPEXP_BASE int
157 wxCRT_VsnprintfA(char *buf, size_t len, const char *format, va_list argptr);
158 #define wxUSE_WXVSNPRINTFA 1
159 #else
160 #define wxUSE_WXVSNPRINTFA 0
161 #endif
162
163 // for wxString code, define wxUSE_WXVSNPRINTF to indicate that wx
164 // implementation is used no matter what (in UTF-8 build, either *A or *W
165 // version may be called):
166 #if !wxUSE_UNICODE
167 #define wxUSE_WXVSNPRINTF wxUSE_WXVSNPRINTFA
168 #elif wxUSE_UNICODE_WCHAR
169 #define wxUSE_WXVSNPRINTF wxUSE_WXVSNPRINTFW
170 #elif wxUSE_UTF8_LOCALE_ONLY
171 #define wxUSE_WXVSNPRINTF wxUSE_WXVSNPRINTFA
172 #else // UTF-8 under any locale
173 #define wxUSE_WXVSNPRINTF (wxUSE_WXVSNPRINTFA && wxUSE_WXVSNPRINTFW)
174 #endif
175
176 #define wxCRT_FprintfA fprintf
177 #define wxCRT_PrintfA printf
178 #define wxCRT_VfprintfA vfprintf
179 #define wxCRT_VprintfA vprintf
180 #define wxCRT_VsprintfA vsprintf
181
182 /*
183 In Unicode mode we need to have all standard functions such as wprintf() and
184 so on but not all systems have them so use our own implementations in this
185 case.
186 */
187 #if wxUSE_UNICODE && !defined(wxHAVE_TCHAR_SUPPORT) && !defined(HAVE_WPRINTF)
188 #define wxNEED_WPRINTF
189 #endif
190
191
192 #if defined(wxNEED_WPRINTF)
193 /*
194 we need to implement all wide character printf functions either because
195 we don't have them at all or because they don't have the semantics we
196 need
197 */
198 int wxCRT_PrintfW( const wchar_t *format, ... );
199 int wxCRT_FprintfW( FILE *stream, const wchar_t *format, ... );
200 int wxCRT_VfprintfW( FILE *stream, const wchar_t *format, va_list ap );
201 int wxCRT_VprintfW( const wchar_t *format, va_list ap );
202 int wxCRT_VsprintfW( wchar_t *str, const wchar_t *format, va_list ap );
203 #else /* !wxNEED_WPRINTF */
204 #define wxCRT_FprintfW fwprintf
205 #define wxCRT_PrintfW wprintf
206 #define wxCRT_VfprintfW vfwprintf
207 #define wxCRT_VprintfW vwprintf
208
209 #if defined(__WINDOWS__) && !defined(HAVE_VSWPRINTF)
210 // only non-standard vswprintf() without buffer size argument can be used here
211 #define wxCRT_VsprintfW vswprintf
212 #endif
213 #endif /* wxNEED_WPRINTF */
214
215
216 /* Required for wxScanf() etc. */
217 #define wxCRT_ScanfA scanf
218 #define wxCRT_SscanfA sscanf
219 #define wxCRT_FscanfA fscanf
220 #define wxCRT_VsscanfA vsscanf
221
222 #if defined(wxNEED_WPRINTF)
223 int wxCRT_ScanfW(const wchar_t *format, ...);
224 int wxCRT_SscanfW(const wchar_t *str, const wchar_t *format, ...);
225 int wxCRT_FscanfW(FILE *stream, const wchar_t *format, ...);
226 int wxCRT_VsscanfW(const wchar_t *str, const wchar_t *format, va_list ap);
227 #else
228 #define wxCRT_ScanfW wxVMS_USE_STD wscanf
229 #define wxCRT_SscanfW wxVMS_USE_STD swscanf
230 #define wxCRT_FscanfW wxVMS_USE_STD fwscanf
231 #define wxCRT_VsscanfW wxVMS_USE_STD vswscanf
232 #endif
233
234 // ----------------------------------------------------------------------------
235 // user-friendly wrappers to CRT functions
236 // ----------------------------------------------------------------------------
237
238 #ifdef __WATCOMC__
239 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
240 #define wxPrintf wxPrintf_Impl
241 #define wxFprintf wxFprintf_Impl
242 #define wxSprintf wxSprintf_Impl
243 #define wxSnprintf wxSnprintf_Impl
244 #endif
245
246 // FIXME-UTF8: remove this
247 #if wxUSE_UNICODE
248 #define wxCRT_PrintfNative wxCRT_PrintfW
249 #define wxCRT_FprintfNative wxCRT_FprintfW
250 #else
251 #define wxCRT_PrintfNative wxCRT_PrintfA
252 #define wxCRT_FprintfNative wxCRT_FprintfA
253 #endif
254
255 WX_DEFINE_VARARG_FUNC(int, wxPrintf, 1, (const wxFormatString&),
256 wxCRT_PrintfNative, wxCRT_PrintfA)
257 WX_DEFINE_VARARG_FUNC(int, wxFprintf, 2, (FILE*, const wxFormatString&),
258 wxCRT_FprintfNative, wxCRT_FprintfA)
259
260 // va_list versions of printf functions simply forward to the respective
261 // CRT function; note that they assume that va_list was created using
262 // wxArgNormalizer<T>!
263 #if wxUSE_UNICODE_UTF8
264 #if wxUSE_UTF8_LOCALE_ONLY
265 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
266 return implA args
267 #else
268 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
269 if ( wxLocaleIsUtf8 ) return implA args; \
270 else return implW args
271 #endif
272 #elif wxUSE_UNICODE_WCHAR
273 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
274 return implW args
275 #else // ANSI
276 #define WX_VARARG_VFOO_IMPL(args, implW, implA) \
277 return implA args
278 #endif
279
280 inline int
281 wxVprintf(const wxString& format, va_list ap)
282 {
283 WX_VARARG_VFOO_IMPL((wxFormatString(format), ap),
284 wxCRT_VprintfW, wxCRT_VprintfA);
285 }
286
287 inline int
288 wxVfprintf(FILE *f, const wxString& format, va_list ap)
289 {
290 WX_VARARG_VFOO_IMPL((f, wxFormatString(format), ap),
291 wxCRT_VfprintfW, wxCRT_VfprintfA);
292 }
293
294 #undef WX_VARARG_VFOO_IMPL
295
296
297 // wxSprintf() and friends have to be implemented in two forms, one for
298 // writing to char* buffer and one for writing to wchar_t*:
299
300 #if !wxUSE_UTF8_LOCALE_ONLY
301 int WXDLLIMPEXP_BASE wxDoSprintfWchar(char *str, const wxChar *format, ...);
302 #endif
303 #if wxUSE_UNICODE_UTF8
304 int WXDLLIMPEXP_BASE wxDoSprintfUtf8(char *str, const char *format, ...);
305 #endif
306 WX_DEFINE_VARARG_FUNC(int, wxSprintf, 2, (char*, const wxFormatString&),
307 wxDoSprintfWchar, wxDoSprintfUtf8)
308
309 int WXDLLIMPEXP_BASE
310 wxVsprintf(char *str, const wxString& format, va_list argptr);
311
312 #if !wxUSE_UTF8_LOCALE_ONLY
313 int WXDLLIMPEXP_BASE wxDoSnprintfWchar(char *str, size_t size, const wxChar *format, ...);
314 #endif
315 #if wxUSE_UNICODE_UTF8
316 int WXDLLIMPEXP_BASE wxDoSnprintfUtf8(char *str, size_t size, const char *format, ...);
317 #endif
318 WX_DEFINE_VARARG_FUNC(int, wxSnprintf, 3, (char*, size_t, const wxFormatString&),
319 wxDoSnprintfWchar, wxDoSnprintfUtf8)
320
321 int WXDLLIMPEXP_BASE
322 wxVsnprintf(char *str, size_t size, const wxString& format, va_list argptr);
323
324 #if wxUSE_UNICODE
325
326 #if !wxUSE_UTF8_LOCALE_ONLY
327 int WXDLLIMPEXP_BASE wxDoSprintfWchar(wchar_t *str, const wxChar *format, ...);
328 #endif
329 #if wxUSE_UNICODE_UTF8
330 int WXDLLIMPEXP_BASE wxDoSprintfUtf8(wchar_t *str, const char *format, ...);
331 #endif
332 WX_DEFINE_VARARG_FUNC(int, wxSprintf, 2, (wchar_t*, const wxFormatString&),
333 wxDoSprintfWchar, wxDoSprintfUtf8)
334
335 int WXDLLIMPEXP_BASE
336 wxVsprintf(wchar_t *str, const wxString& format, va_list argptr);
337
338 #if !wxUSE_UTF8_LOCALE_ONLY
339 int WXDLLIMPEXP_BASE wxDoSnprintfWchar(wchar_t *str, size_t size, const wxChar *format, ...);
340 #endif
341 #if wxUSE_UNICODE_UTF8
342 int WXDLLIMPEXP_BASE wxDoSnprintfUtf8(wchar_t *str, size_t size, const char *format, ...);
343 #endif
344 WX_DEFINE_VARARG_FUNC(int, wxSnprintf, 3, (wchar_t*, size_t, const wxFormatString&),
345 wxDoSnprintfWchar, wxDoSnprintfUtf8)
346
347 int WXDLLIMPEXP_BASE
348 wxVsnprintf(wchar_t *str, size_t size, const wxString& format, va_list argptr);
349
350 #endif // wxUSE_UNICODE
351
352 #ifdef __WATCOMC__
353 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
354 //
355 // fortunately, OpenWatcom implements __VA_ARGS__, so we can provide macros
356 // that cast the format argument to wxString:
357 #undef wxPrintf
358 #undef wxFprintf
359 #undef wxSprintf
360 #undef wxSnprintf
361
362 #define wxPrintf(fmt, ...) \
363 wxPrintf_Impl(wxFormatString(fmt), __VA_ARGS__)
364 #define wxFprintf(f, fmt, ...) \
365 wxFprintf_Impl(f, wxFormatString(fmt), __VA_ARGS__)
366 #define wxSprintf(s, fmt, ...) \
367 wxSprintf_Impl(s, wxFormatString(fmt), __VA_ARGS__)
368 #define wxSnprintf(s, n, fmt, ...) \
369 wxSnprintf_Impl(s, n, wxFormatString(fmt), __VA_ARGS__)
370 #endif // __WATCOMC__
371
372
373 // We can't use wxArgNormalizer<T> for variadic arguments to wxScanf() etc.
374 // because they are writable, so instead of providing friendly template
375 // vararg-like functions, we just provide both char* and wchar_t* variants
376 // of these functions. The type of output variadic arguments for %s must match
377 // the type of 'str' and 'format' arguments.
378 //
379 // For compatibility with earlier wx versions, we also provide wxSscanf()
380 // version with the first argument (input string) wxString; for this version,
381 // the type of output string values is determined by the type of format string
382 // only.
383
384 #define _WX_SCANFUNC_EXTRACT_ARGS_1(x) x
385 #define _WX_SCANFUNC_EXTRACT_ARGS_2(x,y) x, y
386 #define _WX_SCANFUNC_EXTRACT_ARGS(N, args) _WX_SCANFUNC_EXTRACT_ARGS_##N args
387
388 #define _WX_VARARG_PASS_WRITABLE(i) a##i
389
390 #define _WX_DEFINE_SCANFUNC(N, dummy1, name, impl, passfixed, numfixed, fixed)\
391 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
392 int name(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, fixed), \
393 _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \
394 { \
395 return impl(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, passfixed), \
396 _WX_VARARG_JOIN(N, _WX_VARARG_PASS_WRITABLE)); \
397 }
398
399 #define WX_DEFINE_SCANFUNC(name, numfixed, fixed, impl, passfixed) \
400 inline int name(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, fixed)) \
401 { \
402 return impl(_WX_SCANFUNC_EXTRACT_ARGS(numfixed, passfixed)); \
403 } \
404 _WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
405 _WX_DEFINE_SCANFUNC, \
406 dummy1, name, impl, passfixed, numfixed, fixed)
407
408 // this is needed to normalize the format string, see src/common/strvararg.cpp
409 // for more details
410 #ifdef __WINDOWS__
411 #define wxScanfConvertFormatW(fmt) fmt
412 #else
413 const wxWCharBuffer
414 WXDLLIMPEXP_BASE wxScanfConvertFormatW(const wchar_t *format);
415 #endif
416
417 WX_DEFINE_SCANFUNC(wxScanf, 1, (const char *format),
418 wxCRT_ScanfA, (format))
419 WX_DEFINE_SCANFUNC(wxScanf, 1, (const wchar_t *format),
420 wxCRT_ScanfW, (wxScanfConvertFormatW(format)))
421
422 WX_DEFINE_SCANFUNC(wxFscanf, 2, (FILE *stream, const char *format),
423 wxCRT_FscanfA, (stream, format))
424 WX_DEFINE_SCANFUNC(wxFscanf, 2, (FILE *stream, const wchar_t *format),
425 wxCRT_FscanfW, (stream, wxScanfConvertFormatW(format)))
426
427 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const char *str, const char *format),
428 wxCRT_SscanfA, (str, format))
429 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wchar_t *str, const wchar_t *format),
430 wxCRT_SscanfW, (str, wxScanfConvertFormatW(format)))
431 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxCharBuffer& str, const char *format),
432 wxCRT_SscanfA, (str.data(), format))
433 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxWCharBuffer& str, const wchar_t *format),
434 wxCRT_SscanfW, (str.data(), wxScanfConvertFormatW(format)))
435 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxString& str, const char *format),
436 wxCRT_SscanfA, (str.mb_str(), format))
437 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxString& str, const wchar_t *format),
438 wxCRT_SscanfW, (str.wc_str(), wxScanfConvertFormatW(format)))
439 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxCStrData& str, const char *format),
440 wxCRT_SscanfA, (str.AsCharBuf(), format))
441 WX_DEFINE_SCANFUNC(wxSscanf, 2, (const wxCStrData& str, const wchar_t *format),
442 wxCRT_SscanfW, (str.AsWCharBuf(), wxScanfConvertFormatW(format)))
443
444 // Visual C++ doesn't provide vsscanf()
445 #ifndef __VISUALC___
446 int WXDLLIMPEXP_BASE wxVsscanf(const char *str, const char *format, va_list ap);
447 int WXDLLIMPEXP_BASE wxVsscanf(const wchar_t *str, const wchar_t *format, va_list ap);
448 int WXDLLIMPEXP_BASE wxVsscanf(const wxCharBuffer& str, const char *format, va_list ap);
449 int WXDLLIMPEXP_BASE wxVsscanf(const wxWCharBuffer& str, const wchar_t *format, va_list ap);
450 int WXDLLIMPEXP_BASE wxVsscanf(const wxString& str, const char *format, va_list ap);
451 int WXDLLIMPEXP_BASE wxVsscanf(const wxString& str, const wchar_t *format, va_list ap);
452 int WXDLLIMPEXP_BASE wxVsscanf(const wxCStrData& str, const char *format, va_list ap);
453 int WXDLLIMPEXP_BASE wxVsscanf(const wxCStrData& str, const wchar_t *format, va_list ap);
454 #endif // !__VISUALC__
455
456 #endif /* _WX_WXCRTVARARG_H_ */