]> git.saurik.com Git - wxWidgets.git/blob - src/common/msgout.cpp
added Open() mode parameter
[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(__WINDOWS__)
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 #ifdef __WINDOWS__
96
97 // check if we're running in a console under Windows
98 static inline bool IsInConsole()
99 {
100 #ifdef __WXWINCE__
101 return false;
102 #else // !__WXWINCE__
103 HANDLE hStdErr = ::GetStdHandle(STD_ERROR_HANDLE);
104 return hStdErr && hStdErr != INVALID_HANDLE_VALUE;
105 #endif // __WXWINCE__/!__WXWINCE__
106 }
107
108 #endif // __WINDOWS__
109
110 void wxMessageOutputBest::Printf(const wxChar* format, ...)
111 {
112 va_list args;
113 va_start(args, format);
114 wxString out;
115
116 out.PrintfV(format, args);
117 va_end(args);
118
119 #ifdef __WINDOWS__
120 if ( !IsInConsole() )
121 {
122 ::MessageBox(NULL, out, _T("wxWidgets"), MB_ICONINFORMATION | MB_OK);
123 }
124 else
125 #endif // __WINDOWS__/!__WINDOWS__
126 {
127 fprintf(stderr, "%s", (const char*) out.mb_str());
128 }
129 }
130
131 // ----------------------------------------------------------------------------
132 // wxMessageOutputStderr
133 // ----------------------------------------------------------------------------
134
135 void wxMessageOutputStderr::Printf(const wxChar* format, ...)
136 {
137 va_list args;
138 va_start(args, format);
139 wxString out;
140
141 out.PrintfV(format, args);
142 va_end(args);
143
144 fprintf(stderr, "%s", (const char*) out.mb_str());
145 }
146
147 // ----------------------------------------------------------------------------
148 // wxMessageOutputDebug
149 // ----------------------------------------------------------------------------
150
151 void wxMessageOutputDebug::Printf(const wxChar* format, ...)
152 {
153 wxString out;
154
155 va_list args;
156 va_start(args, format);
157
158 out.PrintfV(format, args);
159 va_end(args);
160
161 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
162 out.Replace(wxT("\t"), wxT(" "));
163 out.Replace(wxT("\n"), wxT("\r\n"));
164 ::OutputDebugString(out);
165 #elif defined(__WXMAC__) && !defined(__DARWIN__)
166 if ( wxIsDebuggerRunning() )
167 {
168 Str255 pstr;
169 wxString output = out + wxT(";g") ;
170 wxMacStringToPascal(output.c_str(), pstr);
171
172 #ifdef __powerc
173 DebugStr(pstr);
174 #else
175 SysBreakStr(pstr);
176 #endif
177 }
178 #else
179 wxFputs( out , stderr ) ;
180 if ( out.Right(1) != wxT("\n") )
181 wxFputs( wxT("\n") , stderr ) ;
182 fflush( stderr ) ;
183 #endif // platform
184 }
185
186 // ----------------------------------------------------------------------------
187 // wxMessageOutputLog
188 // ----------------------------------------------------------------------------
189
190 void wxMessageOutputLog::Printf(const wxChar* format, ...)
191 {
192 wxString out;
193
194 va_list args;
195 va_start(args, format);
196
197 out.PrintfV(format, args);
198 va_end(args);
199
200 out.Replace(wxT("\t"), wxT(" "));
201
202 ::wxLogMessage(wxT("%s"), out.c_str());
203 }
204
205 #endif // wxUSE_BASE
206
207 // ----------------------------------------------------------------------------
208 // wxMessageOutputMessageBox
209 // ----------------------------------------------------------------------------
210
211 #if wxUSE_GUI
212
213 void wxMessageOutputMessageBox::Printf(const wxChar* format, ...)
214 {
215 va_list args;
216 va_start(args, format);
217 wxString out;
218
219 out.PrintfV(format, args);
220 va_end(args);
221
222 // the native MSW msg box understands the TABs, others don't
223 #ifndef __WXMSW__
224 out.Replace(wxT("\t"), wxT(" "));
225 #endif
226
227 wxString title;
228 if ( wxTheApp )
229 title.Printf(_("%s message"), wxTheApp->GetAppName().c_str());
230
231 ::wxMessageBox(out, title);
232 }
233
234 #endif // wxUSE_GUI
235