]> git.saurik.com Git - wxWidgets.git/blob - src/common/msgout.cpp
wxMessageBox off the main thread lost result code.
[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 // Copyright: (c) the wxWidgets team
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ---------------------------------------------------------------------------
16 // headers
17 // ---------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #if defined(__BORLANDC__)
23 #pragma hdrstop
24 #endif
25
26 #ifndef WX_PRECOMP
27 #include "wx/string.h"
28 #include "wx/ffile.h"
29 #include "wx/app.h"
30 #include "wx/intl.h"
31 #include "wx/log.h"
32 #if wxUSE_GUI
33 #include "wx/msgdlg.h"
34 #endif // wxUSE_GUI
35 #endif
36
37 #include "wx/msgout.h"
38 #include "wx/apptrait.h"
39 #include <stdarg.h>
40 #include <stdio.h>
41
42 #if defined(__WINDOWS__)
43 #include "wx/msw/private.h"
44 #endif
45
46 // ===========================================================================
47 // implementation
48 // ===========================================================================
49
50 #if wxUSE_BASE
51
52 // ----------------------------------------------------------------------------
53 // wxMessageOutput
54 // ----------------------------------------------------------------------------
55
56 wxMessageOutput* wxMessageOutput::ms_msgOut = 0;
57
58 wxMessageOutput* wxMessageOutput::Get()
59 {
60 if ( !ms_msgOut && wxTheApp )
61 {
62 ms_msgOut = wxTheApp->GetTraits()->CreateMessageOutput();
63 }
64
65 return ms_msgOut;
66 }
67
68 wxMessageOutput* wxMessageOutput::Set(wxMessageOutput* msgout)
69 {
70 wxMessageOutput* old = ms_msgOut;
71 ms_msgOut = msgout;
72 return old;
73 }
74
75 #if !wxUSE_UTF8_LOCALE_ONLY
76 void wxMessageOutput::DoPrintfWchar(const wxChar *format, ...)
77 {
78 va_list args;
79 va_start(args, format);
80 wxString out;
81
82 out.PrintfV(format, args);
83 va_end(args);
84
85 Output(out);
86 }
87 #endif // !wxUSE_UTF8_LOCALE_ONLY
88
89 #if wxUSE_UNICODE_UTF8
90 void wxMessageOutput::DoPrintfUtf8(const char *format, ...)
91 {
92 va_list args;
93 va_start(args, format);
94 wxString out;
95
96 out.PrintfV(format, args);
97 va_end(args);
98
99 Output(out);
100 }
101 #endif // wxUSE_UNICODE_UTF8
102
103 // ----------------------------------------------------------------------------
104 // wxMessageOutputBest
105 // ----------------------------------------------------------------------------
106
107 void wxMessageOutputBest::Output(const wxString& str)
108 {
109 #ifdef __WINDOWS__
110 // decide whether to use console output or not
111 wxAppTraits * const traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
112 const bool hasStderr = traits ? traits->CanUseStderr() : false;
113
114 if ( !(m_flags & wxMSGOUT_PREFER_MSGBOX) )
115 {
116 if ( hasStderr && traits->WriteToStderr(AppendLineFeedIfNeeded(str)) )
117 return;
118 }
119
120 wxString title;
121 if ( wxTheApp )
122 title = wxTheApp->GetAppDisplayName();
123 else // Use some title to avoid default "Error"
124 title = _("Message");
125
126 ::MessageBox(NULL, str.t_str(), title.t_str(), MB_ICONINFORMATION | MB_OK);
127 #else // !__WINDOWS__
128 wxUnusedVar(m_flags);
129
130 // TODO: use the native message box for the other ports too
131 wxMessageOutputStderr::Output(str);
132 #endif // __WINDOWS__/!__WINDOWS__
133 }
134
135 // ----------------------------------------------------------------------------
136 // wxMessageOutputStderr
137 // ----------------------------------------------------------------------------
138
139 wxString wxMessageOutputStderr::AppendLineFeedIfNeeded(const wxString& str)
140 {
141 wxString strLF(str);
142 if ( strLF.empty() || *strLF.rbegin() != '\n' )
143 strLF += '\n';
144
145 return strLF;
146 }
147
148 void wxMessageOutputStderr::Output(const wxString& str)
149 {
150 const wxString strWithLF = AppendLineFeedIfNeeded(str);
151 const wxWX2MBbuf buf = strWithLF.mb_str();
152
153 if ( buf )
154 fprintf(m_fp, "%s", (const char*) buf);
155 else // print at least something
156 fprintf(m_fp, "%s", (const char*) strWithLF.ToAscii());
157
158 fflush(m_fp);
159 }
160
161 // ----------------------------------------------------------------------------
162 // wxMessageOutputDebug
163 // ----------------------------------------------------------------------------
164
165 void wxMessageOutputDebug::Output(const wxString& str)
166 {
167 #if defined(__WINDOWS__) && !defined(__WXMICROWIN__)
168 wxString out(AppendLineFeedIfNeeded(str));
169 out.Replace(wxT("\t"), wxT(" "));
170 out.Replace(wxT("\n"), wxT("\r\n"));
171 ::OutputDebugString(out.t_str());
172 #else
173 // TODO: use native debug output function for the other ports too
174 wxMessageOutputStderr::Output(str);
175 #endif // platform
176 }
177
178 // ----------------------------------------------------------------------------
179 // wxMessageOutputLog
180 // ----------------------------------------------------------------------------
181
182 void wxMessageOutputLog::Output(const wxString& str)
183 {
184 wxString out(str);
185
186 out.Replace(wxT("\t"), wxT(" "));
187
188 wxLogMessage(wxT("%s"), out.c_str());
189 }
190
191 #endif // wxUSE_BASE
192
193 // ----------------------------------------------------------------------------
194 // wxMessageOutputMessageBox
195 // ----------------------------------------------------------------------------
196
197 #if wxUSE_GUI && wxUSE_MSGDLG
198
199 extern WXDLLEXPORT_DATA(const char) wxMessageBoxCaptionStr[] = "Message";
200
201 void wxMessageOutputMessageBox::Output(const wxString& str)
202 {
203 wxString out(str);
204
205 // the native MSW msg box understands the TABs, others don't
206 #ifndef __WINDOWS__
207 out.Replace(wxT("\t"), wxT(" "));
208 #endif
209
210 wxString title = wxT("wxWidgets") ;
211 if (wxTheApp) title = wxTheApp->GetAppDisplayName();
212
213 ::wxMessageBox(out, title);
214 }
215
216 #endif // wxUSE_GUI