added wxMessageOutputBest which tries to show the message to the user in the best...
[wxWidgets.git] / src / common / msgout.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/msgout.cpp
3 // Purpose: wxMessageOutput implementation
4 // Author: Mattia Barbon
5 // Modified by:
6 // Created: 17.07.02
7 // RCS-ID: $Id$
8 // Copyright: (c) the wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ---------------------------------------------------------------------------
17 // headers
18 // ---------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) && !defined(__EMX__)
21 // Some older compilers (such as EMX) cannot handle
22 // #pragma interface/implementation correctly, iff
23 // #pragma implementation is used in _two_ translation
24 // units (as created by e.g. event.cpp compiled for
25 // libwx_base and event.cpp compiled for libwx_gui_core).
26 // So we must not use those pragmas for those compilers in
27 // such files.
28 #pragma implementation "msgout.h"
29 #endif
30
31 // For compilers that support precompilation, includes "wx.h".
32 #include "wx/wxprec.h"
33
34 #if defined(__BORLANDC__)
35 #pragma hdrstop
36 #endif
37
38 #ifndef WX_PRECOMP
39 #include "wx/string.h"
40 #include "wx/ffile.h"
41 #include "wx/app.h"
42 #include "wx/intl.h"
43 #if wxUSE_GUI
44 #include "wx/msgdlg.h"
45 #endif // wxUSE_GUI
46 #endif
47
48 #include "wx/msgout.h"
49 #include "wx/apptrait.h"
50 #include "wx/log.h"
51
52 #include <stdarg.h>
53 #include <stdio.h>
54
55 #if defined(__WXMSW__)
56 #include "wx/msw/private.h"
57 #endif
58 #ifdef __WXMAC__
59 #include "wx/mac/private.h"
60 #endif
61
62 // ===========================================================================
63 // implementation
64 // ===========================================================================
65
66 #if wxUSE_BASE
67
68 // ----------------------------------------------------------------------------
69 // wxMessageOutput
70 // ----------------------------------------------------------------------------
71
72 wxMessageOutput* wxMessageOutput::ms_msgOut = 0;
73
74 wxMessageOutput* wxMessageOutput::Get()
75 {
76 if ( !ms_msgOut && wxTheApp )
77 {
78 ms_msgOut = wxTheApp->GetTraits()->CreateMessageOutput();
79 }
80
81 return ms_msgOut;
82 }
83
84 wxMessageOutput* wxMessageOutput::Set(wxMessageOutput* msgout)
85 {
86 wxMessageOutput* old = ms_msgOut;
87 ms_msgOut = msgout;
88 return old;
89 }
90
91 // ----------------------------------------------------------------------------
92 // wxMessageOutputBest
93 // ----------------------------------------------------------------------------
94
95 void wxMessageOutputBest::Printf(const wxChar* format, ...)
96 {
97 va_list args;
98 va_start(args, format);
99 wxString out;
100
101 out.PrintfV(format, args);
102 va_end(args);
103
104 #ifdef __WINDOWS__
105 ::MessageBox(NULL, out, _T("wxWidgets"), MB_ICONINFORMATION | MB_OK);
106 #else // !__WINDOWS__
107 fprintf(stderr, "%s", (const char*) out.mb_str());
108 #endif // __WINDOWS__/!__WINDOWS__
109 }
110
111 // ----------------------------------------------------------------------------
112 // wxMessageOutputStderr
113 // ----------------------------------------------------------------------------
114
115 void wxMessageOutputStderr::Printf(const wxChar* format, ...)
116 {
117 va_list args;
118 va_start(args, format);
119 wxString out;
120
121 out.PrintfV(format, args);
122 va_end(args);
123
124 fprintf(stderr, "%s", (const char*) out.mb_str());
125 }
126
127 // ----------------------------------------------------------------------------
128 // wxMessageOutputDebug
129 // ----------------------------------------------------------------------------
130
131 void wxMessageOutputDebug::Printf(const wxChar* format, ...)
132 {
133 wxString out;
134
135 va_list args;
136 va_start(args, format);
137
138 out.PrintfV(format, args);
139 va_end(args);
140
141 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
142 out.Replace(wxT("\t"), wxT(" "));
143 out.Replace(wxT("\n"), wxT("\r\n"));
144 ::OutputDebugString(out);
145 #elif defined(__WXMAC__) && !defined(__DARWIN__)
146 if ( wxIsDebuggerRunning() )
147 {
148 Str255 pstr;
149 wxString output = out + wxT(";g") ;
150 wxMacStringToPascal(output.c_str(), pstr);
151
152 #ifdef __powerc
153 DebugStr(pstr);
154 #else
155 SysBreakStr(pstr);
156 #endif
157 }
158 #else
159 wxFputs( out , stderr ) ;
160 if ( out.Right(1) != wxT("\n") )
161 wxFputs( wxT("\n") , stderr ) ;
162 fflush( stderr ) ;
163 #endif // platform
164 }
165
166 // ----------------------------------------------------------------------------
167 // wxMessageOutputLog
168 // ----------------------------------------------------------------------------
169
170 void wxMessageOutputLog::Printf(const wxChar* format, ...)
171 {
172 wxString out;
173
174 va_list args;
175 va_start(args, format);
176
177 out.PrintfV(format, args);
178 va_end(args);
179
180 out.Replace(wxT("\t"), wxT(" "));
181
182 ::wxLogMessage(wxT("%s"), out.c_str());
183 }
184
185 #endif // wxUSE_BASE
186
187 // ----------------------------------------------------------------------------
188 // wxMessageOutputMessageBox
189 // ----------------------------------------------------------------------------
190
191 #if wxUSE_GUI
192
193 void wxMessageOutputMessageBox::Printf(const wxChar* format, ...)
194 {
195 va_list args;
196 va_start(args, format);
197 wxString out;
198
199 out.PrintfV(format, args);
200 va_end(args);
201
202 // the native MSW msg box understands the TABs, others don't
203 #ifndef __WXMSW__
204 out.Replace(wxT("\t"), wxT(" "));
205 #endif
206
207 wxString title;
208 if ( wxTheApp )
209 title.Printf(_("%s message"), wxTheApp->GetAppName().c_str());
210
211 ::wxMessageBox(out, title);
212 }
213
214 #endif // wxUSE_GUI
215