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