msgout.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/msgout.h
  3. // Purpose: wxMessageOutput class. Shows a message to the user
  4. // Author: Mattia Barbon
  5. // Modified by:
  6. // Created: 17.07.02
  7. // Copyright: (c) Mattia Barbon
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_MSGOUT_H_
  11. #define _WX_MSGOUT_H_
  12. // ----------------------------------------------------------------------------
  13. // headers
  14. // ----------------------------------------------------------------------------
  15. #include "wx/defs.h"
  16. #include "wx/chartype.h"
  17. #include "wx/strvararg.h"
  18. // ----------------------------------------------------------------------------
  19. // wxMessageOutput is a class abstracting formatted output target, i.e.
  20. // something you can printf() to
  21. // ----------------------------------------------------------------------------
  22. // NB: VC6 has a bug that causes linker errors if you have template methods
  23. // in a class using __declspec(dllimport). The solution is to split such
  24. // class into two classes, one that contains the template methods and does
  25. // *not* use WXDLLIMPEXP_BASE and another class that contains the rest
  26. // (with DLL linkage).
  27. class wxMessageOutputBase
  28. {
  29. public:
  30. virtual ~wxMessageOutputBase() { }
  31. // show a message to the user
  32. // void Printf(const wxString& format, ...) = 0;
  33. WX_DEFINE_VARARG_FUNC_VOID(Printf, 1, (const wxFormatString&),
  34. DoPrintfWchar, DoPrintfUtf8)
  35. #ifdef __WATCOMC__
  36. // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
  37. WX_VARARG_WATCOM_WORKAROUND(void, Printf, 1, (const wxString&),
  38. (wxFormatString(f1)));
  39. WX_VARARG_WATCOM_WORKAROUND(void, Printf, 1, (const wxCStrData&),
  40. (wxFormatString(f1)));
  41. WX_VARARG_WATCOM_WORKAROUND(void, Printf, 1, (const char*),
  42. (wxFormatString(f1)));
  43. WX_VARARG_WATCOM_WORKAROUND(void, Printf, 1, (const wchar_t*),
  44. (wxFormatString(f1)));
  45. #endif
  46. // called by DoPrintf() to output formatted string but can also be called
  47. // directly if no formatting is needed
  48. virtual void Output(const wxString& str) = 0;
  49. protected:
  50. // NB: this is pure virtual so that it can be implemented in dllexported
  51. // wxMessagOutput class
  52. #if !wxUSE_UTF8_LOCALE_ONLY
  53. virtual void DoPrintfWchar(const wxChar *format, ...) = 0;
  54. #endif
  55. #if wxUSE_UNICODE_UTF8
  56. virtual void DoPrintfUtf8(const char *format, ...) = 0;
  57. #endif
  58. };
  59. #ifdef __VISUALC__
  60. // "non dll-interface class 'wxStringPrintfMixin' used as base interface
  61. // for dll-interface class 'wxString'" -- this is OK in our case
  62. #pragma warning (push)
  63. #pragma warning (disable:4275)
  64. #endif
  65. class WXDLLIMPEXP_BASE wxMessageOutput : public wxMessageOutputBase
  66. {
  67. public:
  68. virtual ~wxMessageOutput() { }
  69. // gets the current wxMessageOutput object (may be NULL during
  70. // initialization or shutdown)
  71. static wxMessageOutput* Get();
  72. // sets the global wxMessageOutput instance; returns the previous one
  73. static wxMessageOutput* Set(wxMessageOutput* msgout);
  74. protected:
  75. #if !wxUSE_UTF8_LOCALE_ONLY
  76. virtual void DoPrintfWchar(const wxChar *format, ...);
  77. #endif
  78. #if wxUSE_UNICODE_UTF8
  79. virtual void DoPrintfUtf8(const char *format, ...);
  80. #endif
  81. private:
  82. static wxMessageOutput* ms_msgOut;
  83. };
  84. #ifdef __VISUALC__
  85. #pragma warning (pop)
  86. #endif
  87. // ----------------------------------------------------------------------------
  88. // implementation which sends output to stderr or specified file
  89. // ----------------------------------------------------------------------------
  90. class WXDLLIMPEXP_BASE wxMessageOutputStderr : public wxMessageOutput
  91. {
  92. public:
  93. wxMessageOutputStderr(FILE *fp = stderr) : m_fp(fp) { }
  94. virtual void Output(const wxString& str);
  95. protected:
  96. // return the string with "\n" appended if it doesn't already terminate
  97. // with it (in which case it's returned unchanged)
  98. wxString AppendLineFeedIfNeeded(const wxString& str);
  99. FILE *m_fp;
  100. };
  101. // ----------------------------------------------------------------------------
  102. // implementation showing the message to the user in "best" possible way:
  103. // uses stderr or message box if available according to the flag given to ctor.
  104. // ----------------------------------------------------------------------------
  105. enum wxMessageOutputFlags
  106. {
  107. wxMSGOUT_PREFER_STDERR = 0, // use stderr if available (this is the default)
  108. wxMSGOUT_PREFER_MSGBOX = 1 // always use message box if available
  109. };
  110. class WXDLLIMPEXP_BASE wxMessageOutputBest : public wxMessageOutputStderr
  111. {
  112. public:
  113. wxMessageOutputBest(wxMessageOutputFlags flags = wxMSGOUT_PREFER_STDERR)
  114. : m_flags(flags) { }
  115. virtual void Output(const wxString& str);
  116. private:
  117. wxMessageOutputFlags m_flags;
  118. };
  119. // ----------------------------------------------------------------------------
  120. // implementation which shows output in a message box
  121. // ----------------------------------------------------------------------------
  122. #if wxUSE_GUI && wxUSE_MSGDLG
  123. class WXDLLIMPEXP_CORE wxMessageOutputMessageBox : public wxMessageOutput
  124. {
  125. public:
  126. wxMessageOutputMessageBox() { }
  127. virtual void Output(const wxString& str);
  128. };
  129. #endif // wxUSE_GUI && wxUSE_MSGDLG
  130. // ----------------------------------------------------------------------------
  131. // implementation using the native way of outputting debug messages
  132. // ----------------------------------------------------------------------------
  133. class WXDLLIMPEXP_BASE wxMessageOutputDebug : public wxMessageOutputStderr
  134. {
  135. public:
  136. wxMessageOutputDebug() { }
  137. virtual void Output(const wxString& str);
  138. };
  139. // ----------------------------------------------------------------------------
  140. // implementation using wxLog (mainly for backwards compatibility)
  141. // ----------------------------------------------------------------------------
  142. class WXDLLIMPEXP_BASE wxMessageOutputLog : public wxMessageOutput
  143. {
  144. public:
  145. wxMessageOutputLog() { }
  146. virtual void Output(const wxString& str);
  147. };
  148. #endif // _WX_MSGOUT_H_