/src/wxwidgets/include/wx/msgout.h
Line | Count | Source (jump to first uncovered line) |
1 | | /////////////////////////////////////////////////////////////////////////////// |
2 | | // Name: wx/msgout.h |
3 | | // Purpose: wxMessageOutput class. Shows a message to the user |
4 | | // Author: Mattia Barbon |
5 | | // Created: 17.07.02 |
6 | | // Copyright: (c) Mattia Barbon |
7 | | // Licence: wxWindows licence |
8 | | /////////////////////////////////////////////////////////////////////////////// |
9 | | |
10 | | #ifndef _WX_MSGOUT_H_ |
11 | | #define _WX_MSGOUT_H_ |
12 | | |
13 | | // ---------------------------------------------------------------------------- |
14 | | // headers |
15 | | // ---------------------------------------------------------------------------- |
16 | | |
17 | | #include "wx/defs.h" |
18 | | #include "wx/string.h" |
19 | | |
20 | | // ---------------------------------------------------------------------------- |
21 | | // wxMessageOutput is a class abstracting formatted output target, i.e. |
22 | | // something you can printf() to |
23 | | // ---------------------------------------------------------------------------- |
24 | | |
25 | | class WXDLLIMPEXP_BASE wxMessageOutput |
26 | | { |
27 | | public: |
28 | 0 | virtual ~wxMessageOutput() = default; |
29 | | |
30 | | // gets the current wxMessageOutput object (may be null during |
31 | | // initialization or shutdown) |
32 | | static wxMessageOutput* Get(); |
33 | | |
34 | | // sets the global wxMessageOutput instance; returns the previous one |
35 | | static wxMessageOutput* Set(wxMessageOutput* msgout); |
36 | | |
37 | | // show a message to the user |
38 | | template <typename FormatString, typename... Targs> |
39 | | void Printf(FormatString format, Targs... args) |
40 | 0 | { |
41 | 0 | Output(wxString::Format(format, args...)); |
42 | 0 | } Unexecuted instantiation: void wxMessageOutput::Printf<char const*, wxString, char const*>(char const*, wxString, char const*) Unexecuted instantiation: void wxMessageOutput::Printf<wxString, wxString, unsigned int>(wxString, wxString, unsigned int) Unexecuted instantiation: void wxMessageOutput::Printf<wchar_t const*, wxCStrData>(wchar_t const*, wxCStrData) |
43 | | |
44 | | // called by DoPrintf() to output formatted string but can also be called |
45 | | // directly if no formatting is needed |
46 | | virtual void Output(const wxString& str) = 0; |
47 | | |
48 | | private: |
49 | | static wxMessageOutput* ms_msgOut; |
50 | | }; |
51 | | |
52 | | // This is equivalent to wxMessageOutput::Get()->Output() but doesn't crash if |
53 | | // there is no message output object (which shouldn't normally happen). |
54 | | inline void wxSafeMessageOutput(const wxString& str) |
55 | 0 | { |
56 | 0 | if ( wxMessageOutput* msgOut = wxMessageOutput::Get() ) |
57 | 0 | msgOut->Output(str); |
58 | 0 | } |
59 | | |
60 | | // ---------------------------------------------------------------------------- |
61 | | // helper mix-in for output targets that can use difference encodings |
62 | | // ---------------------------------------------------------------------------- |
63 | | |
64 | | class WXDLLIMPEXP_BASE wxMessageOutputWithConv |
65 | | { |
66 | | protected: |
67 | | explicit wxMessageOutputWithConv(const wxMBConv& conv) |
68 | 0 | : m_conv(conv.Clone()) |
69 | 0 | { |
70 | 0 | } |
71 | | |
72 | | ~wxMessageOutputWithConv() |
73 | 0 | { |
74 | 0 | delete m_conv; |
75 | 0 | } |
76 | | |
77 | | // return the string with "\n" appended if it doesn't already terminate |
78 | | // with it (in which case it's returned unchanged) |
79 | | wxString AppendLineFeedIfNeeded(const wxString& str); |
80 | | |
81 | | // Prepare the given string for output by appending a new line to it, if |
82 | | // necessary, and converting it to a narrow string using our conversion |
83 | | // object. |
84 | | wxCharBuffer PrepareForOutput(const wxString& str); |
85 | | |
86 | | const wxMBConv* const m_conv; |
87 | | }; |
88 | | |
89 | | // ---------------------------------------------------------------------------- |
90 | | // implementation which sends output to stderr or specified file |
91 | | // ---------------------------------------------------------------------------- |
92 | | |
93 | | class WXDLLIMPEXP_BASE wxMessageOutputStderr : public wxMessageOutput, |
94 | | protected wxMessageOutputWithConv |
95 | | { |
96 | | public: |
97 | | wxMessageOutputStderr(FILE *fp = stderr, |
98 | | const wxMBConv &conv = wxConvWhateverWorks); |
99 | | |
100 | | virtual void Output(const wxString& str) override; |
101 | | |
102 | | protected: |
103 | | FILE *m_fp; |
104 | | |
105 | | wxDECLARE_NO_COPY_CLASS(wxMessageOutputStderr); |
106 | | }; |
107 | | |
108 | | // ---------------------------------------------------------------------------- |
109 | | // implementation showing the message to the user in "best" possible way: |
110 | | // uses stderr or message box if available according to the flag given to ctor. |
111 | | // ---------------------------------------------------------------------------- |
112 | | |
113 | | enum wxMessageOutputFlags |
114 | | { |
115 | | wxMSGOUT_PREFER_STDERR = 0, // use stderr if available (this is the default) |
116 | | wxMSGOUT_PREFER_MSGBOX = 1 // always use message box if available |
117 | | }; |
118 | | |
119 | | class WXDLLIMPEXP_BASE wxMessageOutputBest : public wxMessageOutputStderr |
120 | | { |
121 | | public: |
122 | | wxMessageOutputBest(wxMessageOutputFlags flags = wxMSGOUT_PREFER_STDERR) |
123 | 0 | : m_flags(flags) { } |
124 | | |
125 | | virtual void Output(const wxString& str) override; |
126 | | |
127 | | private: |
128 | | wxMessageOutputFlags m_flags; |
129 | | }; |
130 | | |
131 | | // ---------------------------------------------------------------------------- |
132 | | // implementation which shows output in a message box |
133 | | // ---------------------------------------------------------------------------- |
134 | | |
135 | | #if wxUSE_GUI && wxUSE_MSGDLG |
136 | | |
137 | | class WXDLLIMPEXP_CORE wxMessageOutputMessageBox : public wxMessageOutput |
138 | | { |
139 | | public: |
140 | | wxMessageOutputMessageBox() = default; |
141 | | |
142 | | virtual void Output(const wxString& str) override; |
143 | | }; |
144 | | |
145 | | #endif // wxUSE_GUI && wxUSE_MSGDLG |
146 | | |
147 | | // ---------------------------------------------------------------------------- |
148 | | // implementation using the native way of outputting debug messages |
149 | | // ---------------------------------------------------------------------------- |
150 | | |
151 | | class WXDLLIMPEXP_BASE wxMessageOutputDebug : public wxMessageOutputStderr |
152 | | { |
153 | | public: |
154 | 0 | wxMessageOutputDebug() = default; |
155 | | |
156 | | virtual void Output(const wxString& str) override; |
157 | | }; |
158 | | |
159 | | // ---------------------------------------------------------------------------- |
160 | | // implementation using wxLog (mainly for backwards compatibility) |
161 | | // ---------------------------------------------------------------------------- |
162 | | |
163 | | class WXDLLIMPEXP_BASE wxMessageOutputLog : public wxMessageOutput |
164 | | { |
165 | | public: |
166 | | wxMessageOutputLog() = default; |
167 | | |
168 | | virtual void Output(const wxString& str) override; |
169 | | }; |
170 | | |
171 | | #endif // _WX_MSGOUT_H_ |