]>
Commit | Line | Data |
---|---|---|
e5b50758 | 1 | ///////////////////////////////////////////////////////////////////////////// |
80fdcdb9 | 2 | // Name: wx/msgdlg.h |
e5b50758 | 3 | // Purpose: common header and base class for wxMessageDialog |
99d80019 | 4 | // Author: Julian Smart |
e5b50758 WS |
5 | // Modified by: |
6 | // Created: | |
99d80019 | 7 | // Copyright: (c) Julian Smart |
e5b50758 WS |
8 | // Licence: wxWindows licence |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
34138703 JS |
11 | #ifndef _WX_MSGDLG_H_BASE_ |
12 | #define _WX_MSGDLG_H_BASE_ | |
c801d85f | 13 | |
2ecf902b | 14 | #include "wx/defs.h" |
13a7abf9 | 15 | |
e421922f VZ |
16 | #if wxUSE_MSGDLG |
17 | ||
2afb9e16 | 18 | #include "wx/dialog.h" |
e08931c0 | 19 | #include "wx/stockitem.h" |
2afb9e16 | 20 | |
b8b9e48c | 21 | extern WXDLLIMPEXP_DATA_CORE(const char) wxMessageBoxCaptionStr[]; |
2afb9e16 | 22 | |
e08931c0 VZ |
23 | // ---------------------------------------------------------------------------- |
24 | // wxMessageDialogBase: base class defining wxMessageDialog interface | |
25 | // ---------------------------------------------------------------------------- | |
26 | ||
53a2db12 | 27 | class WXDLLIMPEXP_CORE wxMessageDialogBase : public wxDialog |
e5b50758 | 28 | { |
2afb9e16 | 29 | public: |
e08931c0 VZ |
30 | // helper class for SetXXXLabels() methods: it makes it possible to pass |
31 | // either a stock id (wxID_CLOSE) or a string ("&Close") to them | |
32 | class ButtonLabel | |
33 | { | |
34 | public: | |
35 | // ctors are not explicit, objects of this class can be implicitly | |
36 | // constructed from either stock ids or strings | |
37 | ButtonLabel(int stockId) | |
38 | : m_stockId(stockId) | |
39 | { | |
40 | wxASSERT_MSG( wxIsStockID(stockId), "invalid stock id" ); | |
41 | } | |
42 | ||
43 | ButtonLabel(const wxString& label) | |
44 | : m_label(label), m_stockId(wxID_NONE) | |
45 | { | |
46 | } | |
47 | ||
298c25c3 VZ |
48 | ButtonLabel(const char *label) |
49 | : m_label(label), m_stockId(wxID_NONE) | |
50 | { | |
51 | } | |
52 | ||
53 | ButtonLabel(const wchar_t *label) | |
54 | : m_label(label), m_stockId(wxID_NONE) | |
55 | { | |
56 | } | |
57 | ||
58 | ButtonLabel(const wxCStrData& label) | |
59 | : m_label(label), m_stockId(wxID_NONE) | |
60 | { | |
61 | } | |
62 | ||
e08931c0 VZ |
63 | // default copy ctor and dtor are ok |
64 | ||
65 | // get the string label, whether it was originally specified directly | |
66 | // or as a stock id -- this is only useful for platforms without native | |
67 | // stock items id support | |
68 | wxString GetAsString() const | |
69 | { | |
3b5137c4 VZ |
70 | return m_stockId == wxID_NONE |
71 | ? m_label | |
72 | : wxGetStockLabel(m_stockId, wxSTOCK_FOR_BUTTON); | |
e08931c0 VZ |
73 | } |
74 | ||
75 | // return the stock id or wxID_NONE if this is not a stock label | |
76 | int GetStockId() const { return m_stockId; } | |
77 | ||
78 | private: | |
79 | // the label if explicitly given or empty if this is a stock item | |
80 | const wxString m_label; | |
81 | ||
82 | // the stock item id or wxID_NONE if m_label should be used | |
83 | const int m_stockId; | |
84 | }; | |
85 | ||
2afb9e16 VZ |
86 | // ctors |
87 | wxMessageDialogBase() { m_dialogStyle = 0; } | |
88 | wxMessageDialogBase(wxWindow *parent, | |
89 | const wxString& message, | |
90 | const wxString& caption, | |
91 | long style) | |
92 | : m_message(message), | |
93 | m_caption(caption) | |
94 | { | |
95 | m_parent = parent; | |
96 | SetMessageDialogStyle(style); | |
97 | } | |
98 | ||
99 | // virtual dtor for the base class | |
100 | virtual ~wxMessageDialogBase() { } | |
101 | ||
ede7b017 | 102 | wxString GetCaption() const { return m_caption; } |
2afb9e16 VZ |
103 | |
104 | virtual void SetMessage(const wxString& message) | |
105 | { | |
106 | m_message = message; | |
107 | } | |
108 | ||
ede7b017 VZ |
109 | wxString GetMessage() const { return m_message; } |
110 | ||
111 | void SetExtendedMessage(const wxString& extendedMessage) | |
2afb9e16 VZ |
112 | { |
113 | m_extendedMessage = extendedMessage; | |
114 | } | |
115 | ||
ede7b017 VZ |
116 | wxString GetExtendedMessage() const { return m_extendedMessage; } |
117 | ||
298c25c3 | 118 | // change the dialog style flag |
e5b50758 WS |
119 | void SetMessageDialogStyle(long style) |
120 | { | |
e08931c0 VZ |
121 | wxASSERT_MSG( ((style & wxYES_NO) == wxYES_NO) || !(style & wxYES_NO), |
122 | "wxYES and wxNO may only be used together" ); | |
e5b50758 | 123 | |
f45d6ade VZ |
124 | wxASSERT_MSG( !(style & wxYES) || !(style & wxOK), |
125 | "wxOK and wxYES/wxNO can't be used together" ); | |
126 | ||
c03c6544 VZ |
127 | // It is common to specify just the icon, without wxOK, in the existing |
128 | // code, especially one written by Windows programmers as MB_OK is 0 | |
129 | // and so they're used to omitting wxOK. Don't complain about it but | |
130 | // just add wxOK implicitly for compatibility. | |
131 | if ( !(style & wxYES) && !(style & wxOK) ) | |
132 | style |= wxOK; | |
f45d6ade | 133 | |
e5b50758 | 134 | wxASSERT_MSG( (style & wxID_OK) != wxID_OK, |
e08931c0 | 135 | "wxMessageBox: Did you mean wxOK (and not wxID_OK)?" ); |
e5b50758 | 136 | |
9053c7b9 VZ |
137 | wxASSERT_MSG( !(style & wxNO_DEFAULT) || (style & wxNO), |
138 | "wxNO_DEFAULT is invalid without wxNO" ); | |
41285bc8 | 139 | |
9053c7b9 VZ |
140 | wxASSERT_MSG( !(style & wxCANCEL_DEFAULT) || (style & wxCANCEL), |
141 | "wxCANCEL_DEFAULT is invalid without wxCANCEL" ); | |
41285bc8 | 142 | |
3c9a70dd | 143 | wxASSERT_MSG( !(style & wxCANCEL_DEFAULT) || !(style & wxNO_DEFAULT), |
9053c7b9 | 144 | "only one default button can be specified" ); |
f45d6ade | 145 | |
e5b50758 WS |
146 | m_dialogStyle = style; |
147 | } | |
2afb9e16 VZ |
148 | |
149 | long GetMessageDialogStyle() const { return m_dialogStyle; } | |
150 | ||
23e00c55 | 151 | // customization of the message box buttons |
e08931c0 | 152 | virtual bool SetYesNoLabels(const ButtonLabel& yes,const ButtonLabel& no) |
23e00c55 VZ |
153 | { |
154 | DoSetCustomLabel(m_yes, yes); | |
155 | DoSetCustomLabel(m_no, no); | |
156 | return true; | |
157 | } | |
158 | ||
e08931c0 VZ |
159 | virtual bool SetYesNoCancelLabels(const ButtonLabel& yes, |
160 | const ButtonLabel& no, | |
161 | const ButtonLabel& cancel) | |
23e00c55 VZ |
162 | { |
163 | DoSetCustomLabel(m_yes, yes); | |
164 | DoSetCustomLabel(m_no, no); | |
165 | DoSetCustomLabel(m_cancel, cancel); | |
166 | return true; | |
167 | } | |
168 | ||
e08931c0 | 169 | virtual bool SetOKLabel(const ButtonLabel& ok) |
23e00c55 VZ |
170 | { |
171 | DoSetCustomLabel(m_ok, ok); | |
172 | return true; | |
173 | } | |
174 | ||
e08931c0 VZ |
175 | virtual bool SetOKCancelLabels(const ButtonLabel& ok, |
176 | const ButtonLabel& cancel) | |
23e00c55 VZ |
177 | { |
178 | DoSetCustomLabel(m_ok, ok); | |
179 | DoSetCustomLabel(m_cancel, cancel); | |
180 | return true; | |
181 | } | |
182 | ||
7112cdd1 VZ |
183 | virtual bool SetHelpLabel(const ButtonLabel& help) |
184 | { | |
185 | DoSetCustomLabel(m_help, help); | |
186 | return true; | |
187 | } | |
188 | ||
23e00c55 VZ |
189 | // test if any custom labels were set |
190 | bool HasCustomLabels() const | |
191 | { | |
7112cdd1 | 192 | return !(m_ok.empty() && m_cancel.empty() && m_help.empty() && |
23e00c55 VZ |
193 | m_yes.empty() && m_no.empty()); |
194 | } | |
195 | ||
196 | // these functions return the label to be used for the button which is | |
197 | // either a custom label explicitly set by the user or the default label, | |
198 | // i.e. they always return a valid string | |
92763588 VZ |
199 | wxString GetYesLabel() const |
200 | { return m_yes.empty() ? GetDefaultYesLabel() : m_yes; } | |
201 | wxString GetNoLabel() const | |
202 | { return m_no.empty() ? GetDefaultNoLabel() : m_no; } | |
203 | wxString GetOKLabel() const | |
204 | { return m_ok.empty() ? GetDefaultOKLabel() : m_ok; } | |
23e00c55 | 205 | wxString GetCancelLabel() const |
92763588 | 206 | { return m_cancel.empty() ? GetDefaultCancelLabel() : m_cancel; } |
7112cdd1 VZ |
207 | wxString GetHelpLabel() const |
208 | { return m_help.empty() ? GetDefaultHelpLabel() : m_help; } | |
23e00c55 | 209 | |
ede7b017 | 210 | // based on message dialog style, returns exactly one of: wxICON_NONE, |
67315c8b VZ |
211 | // wxICON_ERROR, wxICON_WARNING, wxICON_QUESTION, wxICON_INFORMATION, |
212 | // wxICON_AUTH_NEEDED | |
213 | virtual long GetEffectiveIcon() const | |
ede7b017 VZ |
214 | { |
215 | if ( m_dialogStyle & wxICON_NONE ) | |
216 | return wxICON_NONE; | |
217 | else if ( m_dialogStyle & wxICON_ERROR ) | |
218 | return wxICON_ERROR; | |
219 | else if ( m_dialogStyle & wxICON_WARNING ) | |
220 | return wxICON_WARNING; | |
221 | else if ( m_dialogStyle & wxICON_QUESTION ) | |
222 | return wxICON_QUESTION; | |
223 | else if ( m_dialogStyle & wxICON_INFORMATION ) | |
224 | return wxICON_INFORMATION; | |
225 | else if ( m_dialogStyle & wxYES ) | |
226 | return wxICON_QUESTION; | |
227 | else | |
228 | return wxICON_INFORMATION; | |
229 | } | |
230 | ||
231 | protected: | |
232 | // for the platforms not supporting separate main and extended messages | |
233 | // this function should be used to combine both of them in a single string | |
234 | wxString GetFullMessage() const | |
235 | { | |
236 | wxString msg = m_message; | |
237 | if ( !m_extendedMessage.empty() ) | |
238 | msg << "\n\n" << m_extendedMessage; | |
239 | ||
240 | return msg; | |
241 | } | |
242 | ||
243 | wxString m_message, | |
244 | m_extendedMessage, | |
245 | m_caption; | |
246 | long m_dialogStyle; | |
247 | ||
23e00c55 VZ |
248 | // this function is called by our public SetXXXLabels() and should assign |
249 | // the value to var with possibly some transformation (e.g. Cocoa version | |
e08931c0 VZ |
250 | // currently uses this to remove any accelerators from the button strings |
251 | // while GTK+ one handles stock items specifically here) | |
252 | virtual void DoSetCustomLabel(wxString& var, const ButtonLabel& label) | |
23e00c55 | 253 | { |
e08931c0 | 254 | var = label.GetAsString(); |
23e00c55 VZ |
255 | } |
256 | ||
d20ba5f8 VZ |
257 | // these functions return the custom label or empty string and should be |
258 | // used only in specific circumstances such as creating the buttons with | |
259 | // these labels (in which case it makes sense to only use a custom label if | |
260 | // it was really given and fall back on stock label otherwise), use the | |
261 | // Get{Yes,No,OK,Cancel}Label() methods above otherwise | |
262 | const wxString& GetCustomYesLabel() const { return m_yes; } | |
263 | const wxString& GetCustomNoLabel() const { return m_no; } | |
264 | const wxString& GetCustomOKLabel() const { return m_ok; } | |
7112cdd1 | 265 | const wxString& GetCustomHelpLabel() const { return m_help; } |
d20ba5f8 VZ |
266 | const wxString& GetCustomCancelLabel() const { return m_cancel; } |
267 | ||
e08931c0 | 268 | private: |
92763588 VZ |
269 | // these functions may be overridden to provide different defaults for the |
270 | // default button labels (this is used by wxGTK) | |
b91d65b1 MB |
271 | virtual wxString GetDefaultYesLabel() const { return wxGetTranslation("Yes"); } |
272 | virtual wxString GetDefaultNoLabel() const { return wxGetTranslation("No"); } | |
273 | virtual wxString GetDefaultOKLabel() const { return wxGetTranslation("OK"); } | |
274 | virtual wxString GetDefaultCancelLabel() const { return wxGetTranslation("Cancel"); } | |
7112cdd1 | 275 | virtual wxString GetDefaultHelpLabel() const { return wxGetTranslation("Help"); } |
92763588 | 276 | |
23e00c55 VZ |
277 | // labels for the buttons, initially empty meaning that the defaults should |
278 | // be used, use GetYes/No/OK/CancelLabel() to access them | |
279 | wxString m_yes, | |
280 | m_no, | |
281 | m_ok, | |
7112cdd1 VZ |
282 | m_cancel, |
283 | m_help; | |
23e00c55 | 284 | |
ede7b017 | 285 | wxDECLARE_NO_COPY_CLASS(wxMessageDialogBase); |
23e00c55 VZ |
286 | }; |
287 | ||
ede7b017 | 288 | #include "wx/generic/msgdlgg.h" |
23e00c55 | 289 | |
2afb9e16 VZ |
290 | #if defined(__WX_COMPILING_MSGDLGG_CPP__) || \ |
291 | defined(__WXUNIVERSAL__) || defined(__WXGPE__) || \ | |
2afb9e16 | 292 | (defined(__WXGTK__) && !defined(__WXGTK20__)) |
2afb9e16 VZ |
293 | |
294 | #define wxMessageDialog wxGenericMessageDialog | |
e9871aaf | 295 | #elif defined(__WXCOCOA__) |
2c6dbc21 | 296 | #include "wx/cocoa/msgdlg.h" |
21709999 | 297 | #elif defined(__WXMSW__) |
2afb9e16 | 298 | #include "wx/msw/msgdlg.h" |
2049ba38 | 299 | #elif defined(__WXMOTIF__) |
2afb9e16 | 300 | #include "wx/motif/msgdlg.h" |
1be7a35c | 301 | #elif defined(__WXGTK20__) |
2afb9e16 | 302 | #include "wx/gtk/msgdlg.h" |
34138703 | 303 | #elif defined(__WXMAC__) |
ef0e9220 | 304 | #include "wx/osx/msgdlg.h" |
1777b9bb | 305 | #elif defined(__WXPM__) |
2afb9e16 | 306 | #include "wx/os2/msgdlg.h" |
c801d85f KB |
307 | #endif |
308 | ||
e421922f VZ |
309 | // ---------------------------------------------------------------------------- |
310 | // wxMessageBox: the simplest way to use wxMessageDialog | |
311 | // ---------------------------------------------------------------------------- | |
312 | ||
53a2db12 | 313 | int WXDLLIMPEXP_CORE wxMessageBox(const wxString& message, |
e5b50758 WS |
314 | const wxString& caption = wxMessageBoxCaptionStr, |
315 | long style = wxOK | wxCENTRE, | |
316 | wxWindow *parent = NULL, | |
317 | int x = wxDefaultCoord, int y = wxDefaultCoord); | |
e421922f VZ |
318 | |
319 | #endif // wxUSE_MSGDLG | |
320 | ||
2afb9e16 | 321 | #endif // _WX_MSGDLG_H_BASE_ |