| 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 | |
| 47 | // =========================================================================== |
| 48 | // implementation |
| 49 | // =========================================================================== |
| 50 | |
| 51 | #if wxUSE_BASE |
| 52 | |
| 53 | // ---------------------------------------------------------------------------- |
| 54 | // wxMessageOutput |
| 55 | // ---------------------------------------------------------------------------- |
| 56 | |
| 57 | wxMessageOutput* wxMessageOutput::ms_msgOut = 0; |
| 58 | |
| 59 | wxMessageOutput* wxMessageOutput::Get() |
| 60 | { |
| 61 | if ( !ms_msgOut && wxTheApp ) |
| 62 | { |
| 63 | ms_msgOut = wxTheApp->GetTraits()->CreateMessageOutput(); |
| 64 | } |
| 65 | |
| 66 | return ms_msgOut; |
| 67 | } |
| 68 | |
| 69 | wxMessageOutput* wxMessageOutput::Set(wxMessageOutput* msgout) |
| 70 | { |
| 71 | wxMessageOutput* old = ms_msgOut; |
| 72 | ms_msgOut = msgout; |
| 73 | return old; |
| 74 | } |
| 75 | |
| 76 | #if !wxUSE_UTF8_LOCALE_ONLY |
| 77 | void wxMessageOutput::DoPrintfWchar(const wxChar *format, ...) |
| 78 | { |
| 79 | va_list args; |
| 80 | va_start(args, format); |
| 81 | wxString out; |
| 82 | |
| 83 | out.PrintfV(format, args); |
| 84 | va_end(args); |
| 85 | |
| 86 | Output(out); |
| 87 | } |
| 88 | #endif // !wxUSE_UTF8_LOCALE_ONLY |
| 89 | |
| 90 | #if wxUSE_UNICODE_UTF8 |
| 91 | void wxMessageOutput::DoPrintfUtf8(const char *format, ...) |
| 92 | { |
| 93 | va_list args; |
| 94 | va_start(args, format); |
| 95 | wxString out; |
| 96 | |
| 97 | out.PrintfV(format, args); |
| 98 | va_end(args); |
| 99 | |
| 100 | Output(out); |
| 101 | } |
| 102 | #endif // wxUSE_UNICODE_UTF8 |
| 103 | |
| 104 | // ---------------------------------------------------------------------------- |
| 105 | // wxMessageOutputBest |
| 106 | // ---------------------------------------------------------------------------- |
| 107 | |
| 108 | void wxMessageOutputBest::Output(const wxString& str) |
| 109 | { |
| 110 | #ifdef __WINDOWS__ |
| 111 | // decide whether to use console output or not |
| 112 | wxAppTraits * const traits = wxTheApp ? wxTheApp->GetTraits() : NULL; |
| 113 | const bool hasStderr = traits ? traits->CanUseStderr() : false; |
| 114 | |
| 115 | if ( !(m_flags & wxMSGOUT_PREFER_MSGBOX) ) |
| 116 | { |
| 117 | if ( hasStderr && traits->WriteToStderr(AppendLineFeedIfNeeded(str)) ) |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | wxString title; |
| 122 | if ( wxTheApp ) |
| 123 | title = wxTheApp->GetAppDisplayName(); |
| 124 | else // Use some title to avoid default "Error" |
| 125 | title = _("Message"); |
| 126 | |
| 127 | ::MessageBox(NULL, str.t_str(), title.t_str(), MB_ICONINFORMATION | MB_OK); |
| 128 | #else // !__WINDOWS__ |
| 129 | wxUnusedVar(m_flags); |
| 130 | |
| 131 | // TODO: use the native message box for the other ports too |
| 132 | wxMessageOutputStderr::Output(str); |
| 133 | #endif // __WINDOWS__/!__WINDOWS__ |
| 134 | } |
| 135 | |
| 136 | // ---------------------------------------------------------------------------- |
| 137 | // wxMessageOutputStderr |
| 138 | // ---------------------------------------------------------------------------- |
| 139 | |
| 140 | wxString wxMessageOutputStderr::AppendLineFeedIfNeeded(const wxString& str) |
| 141 | { |
| 142 | wxString strLF(str); |
| 143 | if ( strLF.empty() || *strLF.rbegin() != '\n' ) |
| 144 | strLF += '\n'; |
| 145 | |
| 146 | return strLF; |
| 147 | } |
| 148 | |
| 149 | void wxMessageOutputStderr::Output(const wxString& str) |
| 150 | { |
| 151 | const wxString strWithLF = AppendLineFeedIfNeeded(str); |
| 152 | const wxWX2MBbuf buf = strWithLF.mb_str(); |
| 153 | |
| 154 | if ( buf ) |
| 155 | fprintf(m_fp, "%s", (const char*) buf); |
| 156 | else // print at least something |
| 157 | fprintf(m_fp, "%s", (const char*) strWithLF.ToAscii()); |
| 158 | |
| 159 | fflush(m_fp); |
| 160 | } |
| 161 | |
| 162 | // ---------------------------------------------------------------------------- |
| 163 | // wxMessageOutputDebug |
| 164 | // ---------------------------------------------------------------------------- |
| 165 | |
| 166 | void wxMessageOutputDebug::Output(const wxString& str) |
| 167 | { |
| 168 | #if defined(__WINDOWS__) && !defined(__WXMICROWIN__) |
| 169 | wxString out(AppendLineFeedIfNeeded(str)); |
| 170 | out.Replace(wxT("\t"), wxT(" ")); |
| 171 | out.Replace(wxT("\n"), wxT("\r\n")); |
| 172 | ::OutputDebugString(out.t_str()); |
| 173 | #else |
| 174 | // TODO: use native debug output function for the other ports too |
| 175 | wxMessageOutputStderr::Output(str); |
| 176 | #endif // platform |
| 177 | } |
| 178 | |
| 179 | // ---------------------------------------------------------------------------- |
| 180 | // wxMessageOutputLog |
| 181 | // ---------------------------------------------------------------------------- |
| 182 | |
| 183 | void wxMessageOutputLog::Output(const wxString& str) |
| 184 | { |
| 185 | wxString out(str); |
| 186 | |
| 187 | out.Replace(wxT("\t"), wxT(" ")); |
| 188 | |
| 189 | wxLogMessage(wxT("%s"), out.c_str()); |
| 190 | } |
| 191 | |
| 192 | #endif // wxUSE_BASE |
| 193 | |
| 194 | // ---------------------------------------------------------------------------- |
| 195 | // wxMessageOutputMessageBox |
| 196 | // ---------------------------------------------------------------------------- |
| 197 | |
| 198 | #if wxUSE_GUI && wxUSE_MSGDLG |
| 199 | |
| 200 | extern WXDLLEXPORT_DATA(const char) wxMessageBoxCaptionStr[] = "Message"; |
| 201 | |
| 202 | void wxMessageOutputMessageBox::Output(const wxString& str) |
| 203 | { |
| 204 | wxString out(str); |
| 205 | |
| 206 | // the native MSW msg box understands the TABs, others don't |
| 207 | #ifndef __WINDOWS__ |
| 208 | out.Replace(wxT("\t"), wxT(" ")); |
| 209 | #endif |
| 210 | |
| 211 | wxString title = wxT("wxWidgets") ; |
| 212 | if (wxTheApp) title = wxTheApp->GetAppDisplayName(); |
| 213 | |
| 214 | ::wxMessageBox(out, title); |
| 215 | } |
| 216 | |
| 217 | #endif // wxUSE_GUI |