]>
Commit | Line | Data |
---|---|---|
74698d3a | 1 | ///////////////////////////////////////////////////////////////////////////// |
e4db172a | 2 | // Name: src/common/msgout.cpp |
74698d3a MB |
3 | // Purpose: wxMessageOutput implementation |
4 | // Author: Mattia Barbon | |
5 | // Modified by: | |
6 | // Created: 17.07.02 | |
77ffb593 | 7 | // Copyright: (c) the wxWidgets team |
65571936 | 8 | // Licence: wxWindows licence |
74698d3a MB |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // --------------------------------------------------------------------------- | |
16 | // headers | |
17 | // --------------------------------------------------------------------------- | |
18 | ||
74698d3a MB |
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" | |
a90253f0 | 30 | #include "wx/intl.h" |
e4db172a | 31 | #include "wx/log.h" |
74698d3a MB |
32 | #if wxUSE_GUI |
33 | #include "wx/msgdlg.h" | |
34 | #endif // wxUSE_GUI | |
35 | #endif | |
36 | ||
37 | #include "wx/msgout.h" | |
7219fc4f | 38 | #include "wx/apptrait.h" |
74698d3a MB |
39 | #include <stdarg.h> |
40 | #include <stdio.h> | |
41 | ||
532d575b | 42 | #if defined(__WINDOWS__) |
e6b02f3f VS |
43 | #include "wx/msw/private.h" |
44 | #endif | |
45 | ||
74698d3a MB |
46 | // =========================================================================== |
47 | // implementation | |
48 | // =========================================================================== | |
49 | ||
ec67cff1 | 50 | #if wxUSE_BASE |
e2478fde VZ |
51 | |
52 | // ---------------------------------------------------------------------------- | |
53 | // wxMessageOutput | |
54 | // ---------------------------------------------------------------------------- | |
55 | ||
74698d3a MB |
56 | wxMessageOutput* wxMessageOutput::ms_msgOut = 0; |
57 | ||
58 | wxMessageOutput* wxMessageOutput::Get() | |
59 | { | |
a69be60b | 60 | if ( !ms_msgOut && wxTheApp ) |
e02911a2 | 61 | { |
dc6d5e38 | 62 | ms_msgOut = wxTheApp->GetTraits()->CreateMessageOutput(); |
e02911a2 MB |
63 | } |
64 | ||
74698d3a MB |
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 | ||
d1f6e2cf VS |
75 | #if !wxUSE_UTF8_LOCALE_ONLY |
76 | void wxMessageOutput::DoPrintfWchar(const wxChar *format, ...) | |
5b077ec7 VS |
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 | } | |
d1f6e2cf VS |
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 | |
5b077ec7 | 102 | |
e12a951e VZ |
103 | // ---------------------------------------------------------------------------- |
104 | // wxMessageOutputBest | |
105 | // ---------------------------------------------------------------------------- | |
106 | ||
5b077ec7 | 107 | void wxMessageOutputBest::Output(const wxString& str) |
e12a951e | 108 | { |
e12a951e | 109 | #ifdef __WINDOWS__ |
784ee7d5 VZ |
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) ) | |
9d03b4ee | 115 | { |
784ee7d5 VZ |
116 | if ( hasStderr && traits->WriteToStderr(AppendLineFeedIfNeeded(str)) ) |
117 | return; | |
9d03b4ee | 118 | } |
5b077ec7 | 119 | |
b4572569 VZ |
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); | |
784ee7d5 | 127 | #else // !__WINDOWS__ |
b4123b5b VZ |
128 | wxUnusedVar(m_flags); |
129 | ||
784ee7d5 VZ |
130 | // TODO: use the native message box for the other ports too |
131 | wxMessageOutputStderr::Output(str); | |
132 | #endif // __WINDOWS__/!__WINDOWS__ | |
e12a951e VZ |
133 | } |
134 | ||
74698d3a MB |
135 | // ---------------------------------------------------------------------------- |
136 | // wxMessageOutputStderr | |
137 | // ---------------------------------------------------------------------------- | |
138 | ||
784ee7d5 VZ |
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 | ||
5b077ec7 | 148 | void wxMessageOutputStderr::Output(const wxString& str) |
74698d3a | 149 | { |
7f173c33 VZ |
150 | const wxString strWithLF = AppendLineFeedIfNeeded(str); |
151 | const wxWX2MBbuf buf = strWithLF.mb_str(); | |
74698d3a | 152 | |
5b077ec7 | 153 | if ( buf ) |
e8c9268b | 154 | fprintf(m_fp, "%s", (const char*) buf); |
5b077ec7 | 155 | else // print at least something |
e8c9268b | 156 | fprintf(m_fp, "%s", (const char*) strWithLF.ToAscii()); |
8570a7b1 VZ |
157 | |
158 | fflush(m_fp); | |
74698d3a MB |
159 | } |
160 | ||
161 | // ---------------------------------------------------------------------------- | |
e2478fde | 162 | // wxMessageOutputDebug |
74698d3a MB |
163 | // ---------------------------------------------------------------------------- |
164 | ||
5b077ec7 | 165 | void wxMessageOutputDebug::Output(const wxString& str) |
74698d3a | 166 | { |
bb5a9514 | 167 | #if defined(__WINDOWS__) && !defined(__WXMICROWIN__) |
784ee7d5 | 168 | wxString out(AppendLineFeedIfNeeded(str)); |
311da78a | 169 | out.Replace(wxT("\t"), wxT(" ")); |
7e6f48d1 | 170 | out.Replace(wxT("\n"), wxT("\r\n")); |
dc874fb4 | 171 | ::OutputDebugString(out.t_str()); |
7873ca31 | 172 | #else |
784ee7d5 VZ |
173 | // TODO: use native debug output function for the other ports too |
174 | wxMessageOutputStderr::Output(str); | |
e2478fde | 175 | #endif // platform |
74698d3a MB |
176 | } |
177 | ||
74698d3a MB |
178 | // ---------------------------------------------------------------------------- |
179 | // wxMessageOutputLog | |
180 | // ---------------------------------------------------------------------------- | |
181 | ||
5b077ec7 | 182 | void wxMessageOutputLog::Output(const wxString& str) |
74698d3a | 183 | { |
5b077ec7 | 184 | wxString out(str); |
74698d3a | 185 | |
311da78a | 186 | out.Replace(wxT("\t"), wxT(" ")); |
a69be60b | 187 | |
af588446 | 188 | wxLogMessage(wxT("%s"), out.c_str()); |
74698d3a | 189 | } |
e2478fde | 190 | |
ec67cff1 | 191 | #endif // wxUSE_BASE |
e2478fde VZ |
192 | |
193 | // ---------------------------------------------------------------------------- | |
194 | // wxMessageOutputMessageBox | |
195 | // ---------------------------------------------------------------------------- | |
196 | ||
fae86641 | 197 | #if wxUSE_GUI && wxUSE_MSGDLG |
e2478fde | 198 | |
f313deaa PC |
199 | extern WXDLLEXPORT_DATA(const char) wxMessageBoxCaptionStr[] = "Message"; |
200 | ||
5b077ec7 | 201 | void wxMessageOutputMessageBox::Output(const wxString& str) |
e2478fde | 202 | { |
5b077ec7 | 203 | wxString out(str); |
e2478fde VZ |
204 | |
205 | // the native MSW msg box understands the TABs, others don't | |
bb5a9514 | 206 | #ifndef __WINDOWS__ |
e2478fde VZ |
207 | out.Replace(wxT("\t"), wxT(" ")); |
208 | #endif | |
209 | ||
997c7e46 CE |
210 | wxString title = wxT("wxWidgets") ; |
211 | if (wxTheApp) title = wxTheApp->GetAppDisplayName(); | |
e2478fde VZ |
212 | |
213 | ::wxMessageBox(out, title); | |
214 | } | |
215 | ||
216 | #endif // wxUSE_GUI |