]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: wx/msgdlgg.h | |
3 | // Purpose: common header and base class for wxMessageDialog | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifndef _WX_MSGDLG_H_BASE_ | |
13 | #define _WX_MSGDLG_H_BASE_ | |
14 | ||
15 | #include "wx/defs.h" | |
16 | ||
17 | #if wxUSE_MSGDLG | |
18 | ||
19 | #include "wx/dialog.h" | |
20 | #include "wx/stockitem.h" | |
21 | ||
22 | WXDLLIMPEXP_DATA_CORE(extern const char) wxMessageBoxCaptionStr[]; | |
23 | ||
24 | // ---------------------------------------------------------------------------- | |
25 | // wxMessageDialogBase: base class defining wxMessageDialog interface | |
26 | // ---------------------------------------------------------------------------- | |
27 | ||
28 | class WXDLLIMPEXP_CORE wxMessageDialogBase : public wxDialog | |
29 | { | |
30 | public: | |
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 | ||
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 | ||
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 | ||
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 | |
106 | virtual bool SetYesNoLabels(const ButtonLabel& WXUNUSED(yes), | |
107 | const ButtonLabel& WXUNUSED(no)) | |
108 | { | |
109 | return false; | |
110 | } | |
111 | ||
112 | virtual bool SetYesNoCancelLabels(const ButtonLabel& WXUNUSED(yes), | |
113 | const ButtonLabel& WXUNUSED(no), | |
114 | const ButtonLabel& WXUNUSED(cancel)) | |
115 | { | |
116 | return false; | |
117 | } | |
118 | ||
119 | virtual bool SetOKLabel(const ButtonLabel& WXUNUSED(ok)) | |
120 | { | |
121 | return false; | |
122 | } | |
123 | ||
124 | virtual bool SetOKCancelLabels(const ButtonLabel& WXUNUSED(ok), | |
125 | const ButtonLabel& WXUNUSED(cancel)) | |
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 | ||
140 | // change the dialog style flag | |
141 | void SetMessageDialogStyle(long style) | |
142 | { | |
143 | #ifdef __WXDEBUG__ | |
144 | wxASSERT_MSG( ((style & wxYES_NO) == wxYES_NO) || !(style & wxYES_NO), | |
145 | "wxYES and wxNO may only be used together" ); | |
146 | ||
147 | wxASSERT_MSG( !(style & wxYES) || !(style & wxOK), | |
148 | "wxOK and wxYES/wxNO can't be used together" ); | |
149 | ||
150 | wxASSERT_MSG( (style & wxYES) || (style & wxOK), | |
151 | "one of wxOK and wxYES/wxNO must be used" ); | |
152 | ||
153 | wxASSERT_MSG( (style & wxID_OK) != wxID_OK, | |
154 | "wxMessageBox: Did you mean wxOK (and not wxID_OK)?" ); | |
155 | ||
156 | if ((style & wxNO) == 0) | |
157 | wxASSERT_MSG( !(style & wxNO_DEFAULT), | |
158 | "wxNO_DEFAULT is invalid without wxNO" ); | |
159 | ||
160 | if ((style & wxCANCEL) == 0) | |
161 | wxASSERT_MSG( !(style & wxCANCEL_DEFAULT), | |
162 | "wxCANCEL_DEFAULT is invalid without wxCANCEL" ); | |
163 | ||
164 | if ((style & wxCANCEL_DEFAULT) != 0) | |
165 | wxASSERT_MSG( !(style & wxNO_DEFAULT), | |
166 | "only one default button can be specified" ); | |
167 | if ((style & wxNO_DEFAULT) != 0) | |
168 | wxASSERT_MSG( !(style & wxCANCEL_DEFAULT), | |
169 | "only one default button can be specified" ); | |
170 | #endif | |
171 | ||
172 | m_dialogStyle = style; | |
173 | } | |
174 | ||
175 | protected: | |
176 | long GetMessageDialogStyle() const { return m_dialogStyle; } | |
177 | ||
178 | ||
179 | // for the platforms not supporting separate main and extended messages | |
180 | // this function should be used to combine both of them in a single string | |
181 | wxString GetFullMessage() const | |
182 | { | |
183 | wxString msg = m_message; | |
184 | if ( !m_extendedMessage.empty() ) | |
185 | msg << "\n\n" << m_extendedMessage; | |
186 | ||
187 | return msg; | |
188 | } | |
189 | ||
190 | wxString m_message, | |
191 | m_extendedMessage, | |
192 | m_caption; | |
193 | long m_dialogStyle; | |
194 | ||
195 | DECLARE_NO_COPY_CLASS(wxMessageDialogBase) | |
196 | }; | |
197 | ||
198 | // this is a helper class for native wxMessageDialog implementations which need | |
199 | // to store the custom button labels as member variables and then use them in | |
200 | // ShowModal() (there could conceivably be a port which would have some native | |
201 | // functions for setting these labels immediately and we also don't need to | |
202 | // store them at all if custom labels are not supported, which is why we do | |
203 | // this in a separate class and not wxMessageDialogBase itself) | |
204 | #if defined(__WXCOCOA__) || \ | |
205 | defined(__WXGTK20__) || \ | |
206 | defined(__WXMAC__) || \ | |
207 | defined(__WXMSW__) | |
208 | ||
209 | class WXDLLIMPEXP_CORE wxMessageDialogWithCustomLabels | |
210 | : public wxMessageDialogBase | |
211 | { | |
212 | public: | |
213 | // ctors | |
214 | wxMessageDialogWithCustomLabels() { } | |
215 | wxMessageDialogWithCustomLabels(wxWindow *parent, | |
216 | const wxString& message, | |
217 | const wxString& caption, | |
218 | long style) | |
219 | : wxMessageDialogBase(parent, message, caption, style) | |
220 | { | |
221 | } | |
222 | ||
223 | // customization of the message box buttons | |
224 | virtual bool SetYesNoLabels(const ButtonLabel& yes,const ButtonLabel& no) | |
225 | { | |
226 | DoSetCustomLabel(m_yes, yes); | |
227 | DoSetCustomLabel(m_no, no); | |
228 | return true; | |
229 | } | |
230 | ||
231 | virtual bool SetYesNoCancelLabels(const ButtonLabel& yes, | |
232 | const ButtonLabel& no, | |
233 | const ButtonLabel& cancel) | |
234 | { | |
235 | DoSetCustomLabel(m_yes, yes); | |
236 | DoSetCustomLabel(m_no, no); | |
237 | DoSetCustomLabel(m_cancel, cancel); | |
238 | return true; | |
239 | } | |
240 | ||
241 | virtual bool SetOKLabel(const ButtonLabel& ok) | |
242 | { | |
243 | DoSetCustomLabel(m_ok, ok); | |
244 | return true; | |
245 | } | |
246 | ||
247 | virtual bool SetOKCancelLabels(const ButtonLabel& ok, | |
248 | const ButtonLabel& cancel) | |
249 | { | |
250 | DoSetCustomLabel(m_ok, ok); | |
251 | DoSetCustomLabel(m_cancel, cancel); | |
252 | return true; | |
253 | } | |
254 | ||
255 | protected: | |
256 | // test if any custom labels were set | |
257 | bool HasCustomLabels() const | |
258 | { | |
259 | return !(m_ok.empty() && m_cancel.empty() && | |
260 | m_yes.empty() && m_no.empty()); | |
261 | } | |
262 | ||
263 | // these functions return the label to be used for the button which is | |
264 | // either a custom label explicitly set by the user or the default label, | |
265 | // i.e. they always return a valid string | |
266 | wxString GetYesLabel() const | |
267 | { return m_yes.empty() ? GetDefaultYesLabel() : m_yes; } | |
268 | wxString GetNoLabel() const | |
269 | { return m_no.empty() ? GetDefaultNoLabel() : m_no; } | |
270 | wxString GetOKLabel() const | |
271 | { return m_ok.empty() ? GetDefaultOKLabel() : m_ok; } | |
272 | wxString GetCancelLabel() const | |
273 | { return m_cancel.empty() ? GetDefaultCancelLabel() : m_cancel; } | |
274 | ||
275 | // this function is called by our public SetXXXLabels() and should assign | |
276 | // the value to var with possibly some transformation (e.g. Cocoa version | |
277 | // currently uses this to remove any accelerators from the button strings | |
278 | // while GTK+ one handles stock items specifically here) | |
279 | virtual void DoSetCustomLabel(wxString& var, const ButtonLabel& label) | |
280 | { | |
281 | var = label.GetAsString(); | |
282 | } | |
283 | ||
284 | private: | |
285 | // these functions may be overridden to provide different defaults for the | |
286 | // default button labels (this is used by wxGTK) | |
287 | virtual wxString GetDefaultYesLabel() const { return _("Yes"); } | |
288 | virtual wxString GetDefaultNoLabel() const { return _("No"); } | |
289 | virtual wxString GetDefaultOKLabel() const { return _("OK"); } | |
290 | virtual wxString GetDefaultCancelLabel() const { return _("Cancel"); } | |
291 | ||
292 | // labels for the buttons, initially empty meaning that the defaults should | |
293 | // be used, use GetYes/No/OK/CancelLabel() to access them | |
294 | wxString m_yes, | |
295 | m_no, | |
296 | m_ok, | |
297 | m_cancel; | |
298 | ||
299 | DECLARE_NO_COPY_CLASS(wxMessageDialogWithCustomLabels) | |
300 | }; | |
301 | ||
302 | #endif // ports needing wxMessageDialogWithCustomLabels | |
303 | ||
304 | #if defined(__WX_COMPILING_MSGDLGG_CPP__) || \ | |
305 | defined(__WXUNIVERSAL__) || defined(__WXGPE__) || \ | |
306 | (defined(__WXGTK__) && !defined(__WXGTK20__)) | |
307 | #include "wx/generic/msgdlgg.h" | |
308 | ||
309 | #define wxMessageDialog wxGenericMessageDialog | |
310 | #elif defined(__WXCOCOA__) | |
311 | #include "wx/cocoa/msgdlg.h" | |
312 | #elif defined(__WXPALMOS__) | |
313 | #include "wx/palmos/msgdlg.h" | |
314 | #elif defined(__WXMSW__) | |
315 | #include "wx/msw/msgdlg.h" | |
316 | #elif defined(__WXMOTIF__) | |
317 | #include "wx/motif/msgdlg.h" | |
318 | #elif defined(__WXGTK20__) | |
319 | #include "wx/gtk/msgdlg.h" | |
320 | #elif defined(__WXMAC__) | |
321 | #include "wx/osx/msgdlg.h" | |
322 | #elif defined(__WXPM__) | |
323 | #include "wx/os2/msgdlg.h" | |
324 | #endif | |
325 | ||
326 | // ---------------------------------------------------------------------------- | |
327 | // wxMessageBox: the simplest way to use wxMessageDialog | |
328 | // ---------------------------------------------------------------------------- | |
329 | ||
330 | int WXDLLIMPEXP_CORE wxMessageBox(const wxString& message, | |
331 | const wxString& caption = wxMessageBoxCaptionStr, | |
332 | long style = wxOK | wxCENTRE, | |
333 | wxWindow *parent = NULL, | |
334 | int x = wxDefaultCoord, int y = wxDefaultCoord); | |
335 | ||
336 | #endif // wxUSE_MSGDLG | |
337 | ||
338 | #endif // _WX_MSGDLG_H_BASE_ |