]> git.saurik.com Git - wxWidgets.git/blob - src/common/msgout.cpp
618ace720ea3c7f3adb5820c1526271c527f9b1c
[wxWidgets.git] / src / common / msgout.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #if defined(__BORLANDC__)
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/string.h"
29 #include "wx/ffile.h"
30 #include "wx/app.h"
31 #include "wx/intl.h"
32 #include "wx/log.h"
33 #if wxUSE_GUI
34 #include "wx/msgdlg.h"
35 #endif // wxUSE_GUI
36 #endif
37
38 #include "wx/msgout.h"
39 #include "wx/apptrait.h"
40 #include <stdarg.h>
41 #include <stdio.h>
42
43 #if defined(__WINDOWS__)
44 #include "wx/msw/private.h"
45 #endif
46 #ifdef __WXMAC__
47 #include "wx/mac/private.h"
48 #endif
49
50 // ===========================================================================
51 // implementation
52 // ===========================================================================
53
54 #if wxUSE_BASE
55
56 // ----------------------------------------------------------------------------
57 // wxMessageOutput
58 // ----------------------------------------------------------------------------
59
60 wxMessageOutput* wxMessageOutput::ms_msgOut = 0;
61
62 wxMessageOutput* wxMessageOutput::Get()
63 {
64 if ( !ms_msgOut && wxTheApp )
65 {
66 ms_msgOut = wxTheApp->GetTraits()->CreateMessageOutput();
67 }
68
69 return ms_msgOut;
70 }
71
72 wxMessageOutput* wxMessageOutput::Set(wxMessageOutput* msgout)
73 {
74 wxMessageOutput* old = ms_msgOut;
75 ms_msgOut = msgout;
76 return old;
77 }
78
79 #if !wxUSE_UTF8_LOCALE_ONLY
80 void wxMessageOutput::DoPrintfWchar(const wxChar *format, ...)
81 {
82 va_list args;
83 va_start(args, format);
84 wxString out;
85
86 out.PrintfV(format, args);
87 va_end(args);
88
89 Output(out);
90 }
91 #endif // !wxUSE_UTF8_LOCALE_ONLY
92
93 #if wxUSE_UNICODE_UTF8
94 void wxMessageOutput::DoPrintfUtf8(const char *format, ...)
95 {
96 va_list args;
97 va_start(args, format);
98 wxString out;
99
100 out.PrintfV(format, args);
101 va_end(args);
102
103 Output(out);
104 }
105 #endif // wxUSE_UNICODE_UTF8
106
107 // ----------------------------------------------------------------------------
108 // wxMessageOutputBest
109 // ----------------------------------------------------------------------------
110
111 #ifdef __WINDOWS__
112
113 // check if we're running in a console under Windows
114 static inline bool IsInConsole()
115 {
116 #ifdef __WXWINCE__
117 return false;
118 #else // !__WXWINCE__
119 HANDLE hStdErr = ::GetStdHandle(STD_ERROR_HANDLE);
120 return hStdErr && hStdErr != INVALID_HANDLE_VALUE;
121 #endif // __WXWINCE__/!__WXWINCE__
122 }
123
124 #endif // __WINDOWS__
125
126 void wxMessageOutputBest::Output(const wxString& str)
127 {
128 #ifdef __WINDOWS__
129 if ( !IsInConsole() )
130 {
131 ::MessageBox(NULL, str.wx_str(), _T("wxWidgets"),
132 MB_ICONINFORMATION | MB_OK);
133 }
134 else
135 #endif // __WINDOWS__/!__WINDOWS__
136 {
137 const wxWX2MBbuf buf = str.mb_str();
138
139 if ( buf )
140 fprintf(stderr, "%s", (const char*) buf);
141 else // print at least something
142 fprintf(stderr, "%s", (const char*) str.ToAscii());
143 }
144 }
145
146 // ----------------------------------------------------------------------------
147 // wxMessageOutputStderr
148 // ----------------------------------------------------------------------------
149
150 void wxMessageOutputStderr::Output(const wxString& str)
151 {
152 const wxWX2MBbuf buf = str.mb_str();
153
154 if ( buf )
155 fprintf(stderr, "%s", (const char*) buf);
156 else // print at least something
157 fprintf(stderr, "%s", (const char*) str.ToAscii());
158 }
159
160 // ----------------------------------------------------------------------------
161 // wxMessageOutputDebug
162 // ----------------------------------------------------------------------------
163
164 void wxMessageOutputDebug::Output(const wxString& str)
165 {
166 wxString out(str);
167
168 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
169 out.Replace(wxT("\t"), wxT(" "));
170 out.Replace(wxT("\n"), wxT("\r\n"));
171 ::OutputDebugString(out.wx_str());
172 #elif defined(__WXMAC__) && !defined(__DARWIN__)
173 if ( wxIsDebuggerRunning() )
174 {
175 Str255 pstr;
176 wxString output = out + wxT(";g") ;
177 wxMacStringToPascal(output.c_str(), pstr);
178
179 #ifdef __powerc
180 DebugStr(pstr);
181 #else
182 SysBreakStr(pstr);
183 #endif
184 }
185 #else
186 wxFputs( out , stderr ) ;
187 if ( out.Right(1) != wxT("\n") )
188 wxFputs( wxT("\n") , stderr ) ;
189 fflush( stderr ) ;
190 #endif // platform
191 }
192
193 // ----------------------------------------------------------------------------
194 // wxMessageOutputLog
195 // ----------------------------------------------------------------------------
196
197 void wxMessageOutputLog::Output(const wxString& str)
198 {
199 wxString out(str);
200
201 out.Replace(wxT("\t"), wxT(" "));
202
203 ::wxLogMessage(wxT("%s"), out.c_str());
204 }
205
206 #endif // wxUSE_BASE
207
208 // ----------------------------------------------------------------------------
209 // wxMessageOutputMessageBox
210 // ----------------------------------------------------------------------------
211
212 #if wxUSE_GUI
213
214 void wxMessageOutputMessageBox::Output(const wxString& str)
215 {
216 wxString out(str);
217
218 // the native MSW msg box understands the TABs, others don't
219 #ifndef __WXMSW__
220 out.Replace(wxT("\t"), wxT(" "));
221 #endif
222
223 wxString title;
224 if ( wxTheApp )
225 title.Printf(_("%s message"), wxTheApp->GetAppName().c_str());
226
227 ::wxMessageBox(out, title);
228 }
229
230 #endif // wxUSE_GUI