Fix bug with using uninitialized flags in GetParentForModalDialog().
[wxWidgets.git] / include / wx / msgdlg.h
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
72 ? m_label
73 : wxGetStockLabel(m_stockId, wxSTOCK_FOR_BUTTON);
74 }
75
76 // return the stock id or wxID_NONE if this is not a stock label
77 int GetStockId() const { return m_stockId; }
78
79 private:
80 // the label if explicitly given or empty if this is a stock item
81 const wxString m_label;
82
83 // the stock item id or wxID_NONE if m_label should be used
84 const int m_stockId;
85 };
86
87
88 // ctors
89 wxMessageDialogBase() { m_dialogStyle = 0; }
90 wxMessageDialogBase(wxWindow *parent,
91 const wxString& message,
92 const wxString& caption,
93 long style)
94 : m_message(message),
95 m_caption(caption)
96 {
97 m_parent = parent;
98 SetMessageDialogStyle(style);
99 }
100
101 // virtual dtor for the base class
102 virtual ~wxMessageDialogBase() { }
103
104
105 // methods for setting up more custom message dialogs -- all functions
106 // return false if they're not implemented
107 virtual bool SetYesNoLabels(const ButtonLabel& WXUNUSED(yes),
108 const ButtonLabel& WXUNUSED(no))
109 {
110 return false;
111 }
112
113 virtual bool SetYesNoCancelLabels(const ButtonLabel& WXUNUSED(yes),
114 const ButtonLabel& WXUNUSED(no),
115 const ButtonLabel& WXUNUSED(cancel))
116 {
117 return false;
118 }
119
120 virtual bool SetOKLabel(const ButtonLabel& WXUNUSED(ok))
121 {
122 return false;
123 }
124
125 virtual bool SetOKCancelLabels(const ButtonLabel& WXUNUSED(ok),
126 const ButtonLabel& WXUNUSED(cancel))
127 {
128 return false;
129 }
130
131 virtual void SetMessage(const wxString& message)
132 {
133 m_message = message;
134 }
135
136 virtual void SetExtendedMessage(const wxString& extendedMessage)
137 {
138 m_extendedMessage = extendedMessage;
139 }
140
141 // change the dialog style flag
142 void SetMessageDialogStyle(long style)
143 {
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 wxASSERT_MSG( !(style & wxNO_DEFAULT) || (style & wxNO),
157 "wxNO_DEFAULT is invalid without wxNO" );
158
159 wxASSERT_MSG( !(style & wxCANCEL_DEFAULT) || (style & wxCANCEL),
160 "wxCANCEL_DEFAULT is invalid without wxCANCEL" );
161
162 wxASSERT_MSG( !(style & wxCANCEL_DEFAULT) || !(style & wxNO_DEFAULT),
163 "only one default button can be specified" );
164
165 m_dialogStyle = style;
166 }
167
168 protected:
169 long GetMessageDialogStyle() const { return m_dialogStyle; }
170
171 // based on message dialog style, returns exactly one of: wxICON_NONE,
172 // wxICON_ERROR, wxICON_WARNING, wxICON_QUESTION, wxICON_INFORMATION
173 long GetEffectiveIcon() const
174 {
175 if ( m_dialogStyle & wxICON_NONE )
176 return wxICON_NONE;
177 else if ( m_dialogStyle & wxICON_ERROR )
178 return wxICON_ERROR;
179 else if ( m_dialogStyle & wxICON_WARNING )
180 return wxICON_WARNING;
181 else if ( m_dialogStyle & wxICON_QUESTION )
182 return wxICON_QUESTION;
183 else if ( m_dialogStyle & wxICON_INFORMATION )
184 return wxICON_INFORMATION;
185 else if ( m_dialogStyle & wxYES )
186 return wxICON_QUESTION;
187 else
188 return wxICON_INFORMATION;
189 }
190
191
192 // for the platforms not supporting separate main and extended messages
193 // this function should be used to combine both of them in a single string
194 wxString GetFullMessage() const
195 {
196 wxString msg = m_message;
197 if ( !m_extendedMessage.empty() )
198 msg << "\n\n" << m_extendedMessage;
199
200 return msg;
201 }
202
203 wxString m_message,
204 m_extendedMessage,
205 m_caption;
206 long m_dialogStyle;
207
208 wxDECLARE_NO_COPY_CLASS(wxMessageDialogBase);
209 };
210
211 // this is a helper class for native wxMessageDialog implementations which need
212 // to store the custom button labels as member variables and then use them in
213 // ShowModal() (there could conceivably be a port which would have some native
214 // functions for setting these labels immediately and we also don't need to
215 // store them at all if custom labels are not supported, which is why we do
216 // this in a separate class and not wxMessageDialogBase itself)
217 #if defined(__WXCOCOA__) || \
218 defined(__WXGTK20__) || \
219 defined(__WXMAC__) || \
220 defined(__WXMSW__)
221
222 class WXDLLIMPEXP_CORE wxMessageDialogWithCustomLabels
223 : public wxMessageDialogBase
224 {
225 public:
226 // ctors
227 wxMessageDialogWithCustomLabels() { }
228 wxMessageDialogWithCustomLabels(wxWindow *parent,
229 const wxString& message,
230 const wxString& caption,
231 long style)
232 : wxMessageDialogBase(parent, message, caption, style)
233 {
234 }
235
236 // customization of the message box buttons
237 virtual bool SetYesNoLabels(const ButtonLabel& yes,const ButtonLabel& no)
238 {
239 DoSetCustomLabel(m_yes, yes);
240 DoSetCustomLabel(m_no, no);
241 return true;
242 }
243
244 virtual bool SetYesNoCancelLabels(const ButtonLabel& yes,
245 const ButtonLabel& no,
246 const ButtonLabel& cancel)
247 {
248 DoSetCustomLabel(m_yes, yes);
249 DoSetCustomLabel(m_no, no);
250 DoSetCustomLabel(m_cancel, cancel);
251 return true;
252 }
253
254 virtual bool SetOKLabel(const ButtonLabel& ok)
255 {
256 DoSetCustomLabel(m_ok, ok);
257 return true;
258 }
259
260 virtual bool SetOKCancelLabels(const ButtonLabel& ok,
261 const ButtonLabel& cancel)
262 {
263 DoSetCustomLabel(m_ok, ok);
264 DoSetCustomLabel(m_cancel, cancel);
265 return true;
266 }
267
268 protected:
269 // test if any custom labels were set
270 bool HasCustomLabels() const
271 {
272 return !(m_ok.empty() && m_cancel.empty() &&
273 m_yes.empty() && m_no.empty());
274 }
275
276 // these functions return the label to be used for the button which is
277 // either a custom label explicitly set by the user or the default label,
278 // i.e. they always return a valid string
279 wxString GetYesLabel() const
280 { return m_yes.empty() ? GetDefaultYesLabel() : m_yes; }
281 wxString GetNoLabel() const
282 { return m_no.empty() ? GetDefaultNoLabel() : m_no; }
283 wxString GetOKLabel() const
284 { return m_ok.empty() ? GetDefaultOKLabel() : m_ok; }
285 wxString GetCancelLabel() const
286 { return m_cancel.empty() ? GetDefaultCancelLabel() : m_cancel; }
287
288 // this function is called by our public SetXXXLabels() and should assign
289 // the value to var with possibly some transformation (e.g. Cocoa version
290 // currently uses this to remove any accelerators from the button strings
291 // while GTK+ one handles stock items specifically here)
292 virtual void DoSetCustomLabel(wxString& var, const ButtonLabel& label)
293 {
294 var = label.GetAsString();
295 }
296
297 private:
298 // these functions may be overridden to provide different defaults for the
299 // default button labels (this is used by wxGTK)
300 virtual wxString GetDefaultYesLabel() const { return wxGetTranslation("Yes"); }
301 virtual wxString GetDefaultNoLabel() const { return wxGetTranslation("No"); }
302 virtual wxString GetDefaultOKLabel() const { return wxGetTranslation("OK"); }
303 virtual wxString GetDefaultCancelLabel() const { return wxGetTranslation("Cancel"); }
304
305 // labels for the buttons, initially empty meaning that the defaults should
306 // be used, use GetYes/No/OK/CancelLabel() to access them
307 wxString m_yes,
308 m_no,
309 m_ok,
310 m_cancel;
311
312 wxDECLARE_NO_COPY_CLASS(wxMessageDialogWithCustomLabels);
313 };
314
315 #endif // ports needing wxMessageDialogWithCustomLabels
316
317 #if defined(__WX_COMPILING_MSGDLGG_CPP__) || \
318 defined(__WXUNIVERSAL__) || defined(__WXGPE__) || \
319 (defined(__WXGTK__) && !defined(__WXGTK20__))
320 #include "wx/generic/msgdlgg.h"
321
322 #define wxMessageDialog wxGenericMessageDialog
323 #elif defined(__WXCOCOA__)
324 #include "wx/cocoa/msgdlg.h"
325 #elif defined(__WXPALMOS__)
326 #include "wx/palmos/msgdlg.h"
327 #elif defined(__WXMSW__)
328 #include "wx/msw/msgdlg.h"
329 #elif defined(__WXMOTIF__)
330 #include "wx/motif/msgdlg.h"
331 #elif defined(__WXGTK20__)
332 #include "wx/gtk/msgdlg.h"
333 #elif defined(__WXMAC__)
334 #include "wx/osx/msgdlg.h"
335 #elif defined(__WXPM__)
336 #include "wx/os2/msgdlg.h"
337 #endif
338
339 // ----------------------------------------------------------------------------
340 // wxMessageBox: the simplest way to use wxMessageDialog
341 // ----------------------------------------------------------------------------
342
343 int WXDLLIMPEXP_CORE wxMessageBox(const wxString& message,
344 const wxString& caption = wxMessageBoxCaptionStr,
345 long style = wxOK | wxCENTRE,
346 wxWindow *parent = NULL,
347 int x = wxDefaultCoord, int y = wxDefaultCoord);
348
349 #endif // wxUSE_MSGDLG
350
351 #endif // _WX_MSGDLG_H_BASE_