1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMessageOutput class. Shows a message to the user
4 // Author: Mattia Barbon
7 // Copyright: (c) Mattia Barbon
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
19 #include "wx/chartype.h"
20 #include "wx/strvararg.h"
22 // ----------------------------------------------------------------------------
23 // wxMessageOutput is a class abstracting formatted output target, i.e.
24 // something you can printf() to
25 // ----------------------------------------------------------------------------
27 // NB: VC6 has a bug that causes linker errors if you have template methods
28 // in a class using __declspec(dllimport). The solution is to split such
29 // class into two classes, one that contains the template methods and does
30 // *not* use WXDLLIMPEXP_BASE and another class that contains the rest
31 // (with DLL linkage).
32 class wxMessageOutputBase
35 virtual ~wxMessageOutputBase() { }
37 // show a message to the user
38 // void Printf(const wxString& format, ...) = 0;
39 WX_DEFINE_VARARG_FUNC_VOID(Printf
, 1, (const wxFormatString
&),
40 DoPrintfWchar
, DoPrintfUtf8
)
42 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
43 WX_VARARG_WATCOM_WORKAROUND(void, Printf
, 1, (const wxString
&),
44 (wxFormatString(f1
)));
45 WX_VARARG_WATCOM_WORKAROUND(void, Printf
, 1, (const wxCStrData
&),
46 (wxFormatString(f1
)));
47 WX_VARARG_WATCOM_WORKAROUND(void, Printf
, 1, (const char*),
48 (wxFormatString(f1
)));
49 WX_VARARG_WATCOM_WORKAROUND(void, Printf
, 1, (const wchar_t*),
50 (wxFormatString(f1
)));
53 // called by DoPrintf() to output formatted string but can also be called
54 // directly if no formatting is needed
55 virtual void Output(const wxString
& str
) = 0;
58 // NB: this is pure virtual so that it can be implemented in dllexported
59 // wxMessagOutput class
60 #if !wxUSE_UTF8_LOCALE_ONLY
61 virtual void DoPrintfWchar(const wxChar
*format
, ...) = 0;
63 #if wxUSE_UNICODE_UTF8
64 virtual void DoPrintfUtf8(const char *format
, ...) = 0;
69 // "non dll-interface class 'wxStringPrintfMixin' used as base interface
70 // for dll-interface class 'wxString'" -- this is OK in our case
71 #pragma warning (push)
72 #pragma warning (disable:4275)
75 class WXDLLIMPEXP_BASE wxMessageOutput
: public wxMessageOutputBase
78 virtual ~wxMessageOutput() { }
80 // gets the current wxMessageOutput object (may be NULL during
81 // initialization or shutdown)
82 static wxMessageOutput
* Get();
84 // sets the global wxMessageOutput instance; returns the previous one
85 static wxMessageOutput
* Set(wxMessageOutput
* msgout
);
88 #if !wxUSE_UTF8_LOCALE_ONLY
89 virtual void DoPrintfWchar(const wxChar
*format
, ...);
91 #if wxUSE_UNICODE_UTF8
92 virtual void DoPrintfUtf8(const char *format
, ...);
96 static wxMessageOutput
* ms_msgOut
;
100 #pragma warning (pop)
103 // ----------------------------------------------------------------------------
104 // implementation which sends output to stderr or specified file
105 // ----------------------------------------------------------------------------
107 class WXDLLIMPEXP_BASE wxMessageOutputStderr
: public wxMessageOutput
110 wxMessageOutputStderr(FILE *fp
= stderr
) : m_fp(fp
) { }
112 virtual void Output(const wxString
& str
);
115 // return the string with "\n" appended if it doesn't already terminate
116 // with it (in which case it's returned unchanged)
117 wxString
AppendLineFeedIfNeeded(const wxString
& str
);
122 // ----------------------------------------------------------------------------
123 // implementation showing the message to the user in "best" possible way:
124 // uses stderr or message box if available according to the flag given to ctor.
125 // ----------------------------------------------------------------------------
127 enum wxMessageOutputFlags
129 wxMSGOUT_PREFER_STDERR
= 0, // use stderr if available (this is the default)
130 wxMSGOUT_PREFER_MSGBOX
= 1 // always use message box if available
133 class WXDLLIMPEXP_BASE wxMessageOutputBest
: public wxMessageOutputStderr
136 wxMessageOutputBest(wxMessageOutputFlags flags
= wxMSGOUT_PREFER_STDERR
)
139 virtual void Output(const wxString
& str
);
142 wxMessageOutputFlags m_flags
;
145 // ----------------------------------------------------------------------------
146 // implementation which shows output in a message box
147 // ----------------------------------------------------------------------------
149 #if wxUSE_GUI && wxUSE_MSGDLG
151 class WXDLLIMPEXP_CORE wxMessageOutputMessageBox
: public wxMessageOutput
154 wxMessageOutputMessageBox() { }
156 virtual void Output(const wxString
& str
);
159 #endif // wxUSE_GUI && wxUSE_MSGDLG
161 // ----------------------------------------------------------------------------
162 // implementation using the native way of outputting debug messages
163 // ----------------------------------------------------------------------------
165 class WXDLLIMPEXP_BASE wxMessageOutputDebug
: public wxMessageOutputStderr
168 wxMessageOutputDebug() { }
170 virtual void Output(const wxString
& str
);
173 // ----------------------------------------------------------------------------
174 // implementation using wxLog (mainly for backwards compatibility)
175 // ----------------------------------------------------------------------------
177 class WXDLLIMPEXP_BASE wxMessageOutputLog
: public wxMessageOutput
180 wxMessageOutputLog() { }
182 virtual void Output(const wxString
& str
);
185 #endif // _WX_MSGOUT_H_