]> git.saurik.com Git - wxWidgets.git/blame - include/wx/msgdlg.h
Add functor-taking overload of CallAfter().
[wxWidgets.git] / include / wx / msgdlg.h
CommitLineData
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:
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
b8b9e48c 22extern WXDLLIMPEXP_DATA_CORE(const char) wxMessageBoxCaptionStr[];
2afb9e16 23
e08931c0
VZ
24// ----------------------------------------------------------------------------
25// wxMessageDialogBase: base class defining wxMessageDialog interface
26// ----------------------------------------------------------------------------
27
53a2db12 28class WXDLLIMPEXP_CORE wxMessageDialogBase : public wxDialog
e5b50758 29{
2afb9e16 30public:
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 {
3b5137c4
VZ
71 return m_stockId == wxID_NONE
72 ? m_label
73 : wxGetStockLabel(m_stockId, wxSTOCK_FOR_BUTTON);
e08931c0
VZ
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
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
ede7b017 103 wxString GetCaption() const { return m_caption; }
2afb9e16
VZ
104
105 virtual void SetMessage(const wxString& message)
106 {
107 m_message = message;
108 }
109
ede7b017
VZ
110 wxString GetMessage() const { return m_message; }
111
112 void SetExtendedMessage(const wxString& extendedMessage)
2afb9e16
VZ
113 {
114 m_extendedMessage = extendedMessage;
115 }
116
ede7b017
VZ
117 wxString GetExtendedMessage() const { return m_extendedMessage; }
118
298c25c3 119 // change the dialog style flag
e5b50758
WS
120 void SetMessageDialogStyle(long style)
121 {
e08931c0
VZ
122 wxASSERT_MSG( ((style & wxYES_NO) == wxYES_NO) || !(style & wxYES_NO),
123 "wxYES and wxNO may only be used together" );
e5b50758 124
f45d6ade
VZ
125 wxASSERT_MSG( !(style & wxYES) || !(style & wxOK),
126 "wxOK and wxYES/wxNO can't be used together" );
127
c03c6544
VZ
128 // It is common to specify just the icon, without wxOK, in the existing
129 // code, especially one written by Windows programmers as MB_OK is 0
130 // and so they're used to omitting wxOK. Don't complain about it but
131 // just add wxOK implicitly for compatibility.
132 if ( !(style & wxYES) && !(style & wxOK) )
133 style |= wxOK;
f45d6ade 134
e5b50758 135 wxASSERT_MSG( (style & wxID_OK) != wxID_OK,
e08931c0 136 "wxMessageBox: Did you mean wxOK (and not wxID_OK)?" );
e5b50758 137
9053c7b9
VZ
138 wxASSERT_MSG( !(style & wxNO_DEFAULT) || (style & wxNO),
139 "wxNO_DEFAULT is invalid without wxNO" );
41285bc8 140
9053c7b9
VZ
141 wxASSERT_MSG( !(style & wxCANCEL_DEFAULT) || (style & wxCANCEL),
142 "wxCANCEL_DEFAULT is invalid without wxCANCEL" );
41285bc8 143
3c9a70dd 144 wxASSERT_MSG( !(style & wxCANCEL_DEFAULT) || !(style & wxNO_DEFAULT),
9053c7b9 145 "only one default button can be specified" );
f45d6ade 146
e5b50758
WS
147 m_dialogStyle = style;
148 }
2afb9e16
VZ
149
150 long GetMessageDialogStyle() const { return m_dialogStyle; }
151
23e00c55 152 // customization of the message box buttons
e08931c0 153 virtual bool SetYesNoLabels(const ButtonLabel& yes,const ButtonLabel& no)
23e00c55
VZ
154 {
155 DoSetCustomLabel(m_yes, yes);
156 DoSetCustomLabel(m_no, no);
157 return true;
158 }
159
e08931c0
VZ
160 virtual bool SetYesNoCancelLabels(const ButtonLabel& yes,
161 const ButtonLabel& no,
162 const ButtonLabel& cancel)
23e00c55
VZ
163 {
164 DoSetCustomLabel(m_yes, yes);
165 DoSetCustomLabel(m_no, no);
166 DoSetCustomLabel(m_cancel, cancel);
167 return true;
168 }
169
e08931c0 170 virtual bool SetOKLabel(const ButtonLabel& ok)
23e00c55
VZ
171 {
172 DoSetCustomLabel(m_ok, ok);
173 return true;
174 }
175
e08931c0
VZ
176 virtual bool SetOKCancelLabels(const ButtonLabel& ok,
177 const ButtonLabel& cancel)
23e00c55
VZ
178 {
179 DoSetCustomLabel(m_ok, ok);
180 DoSetCustomLabel(m_cancel, cancel);
181 return true;
182 }
183
7112cdd1
VZ
184 virtual bool SetHelpLabel(const ButtonLabel& help)
185 {
186 DoSetCustomLabel(m_help, help);
187 return true;
188 }
189
23e00c55
VZ
190 // test if any custom labels were set
191 bool HasCustomLabels() const
192 {
7112cdd1 193 return !(m_ok.empty() && m_cancel.empty() && m_help.empty() &&
23e00c55
VZ
194 m_yes.empty() && m_no.empty());
195 }
196
197 // these functions return the label to be used for the button which is
198 // either a custom label explicitly set by the user or the default label,
199 // i.e. they always return a valid string
92763588
VZ
200 wxString GetYesLabel() const
201 { return m_yes.empty() ? GetDefaultYesLabel() : m_yes; }
202 wxString GetNoLabel() const
203 { return m_no.empty() ? GetDefaultNoLabel() : m_no; }
204 wxString GetOKLabel() const
205 { return m_ok.empty() ? GetDefaultOKLabel() : m_ok; }
23e00c55 206 wxString GetCancelLabel() const
92763588 207 { return m_cancel.empty() ? GetDefaultCancelLabel() : m_cancel; }
7112cdd1
VZ
208 wxString GetHelpLabel() const
209 { return m_help.empty() ? GetDefaultHelpLabel() : m_help; }
23e00c55 210
ede7b017 211 // based on message dialog style, returns exactly one of: wxICON_NONE,
67315c8b
VZ
212 // wxICON_ERROR, wxICON_WARNING, wxICON_QUESTION, wxICON_INFORMATION,
213 // wxICON_AUTH_NEEDED
214 virtual long GetEffectiveIcon() const
ede7b017
VZ
215 {
216 if ( m_dialogStyle & wxICON_NONE )
217 return wxICON_NONE;
218 else if ( m_dialogStyle & wxICON_ERROR )
219 return wxICON_ERROR;
220 else if ( m_dialogStyle & wxICON_WARNING )
221 return wxICON_WARNING;
222 else if ( m_dialogStyle & wxICON_QUESTION )
223 return wxICON_QUESTION;
224 else if ( m_dialogStyle & wxICON_INFORMATION )
225 return wxICON_INFORMATION;
226 else if ( m_dialogStyle & wxYES )
227 return wxICON_QUESTION;
228 else
229 return wxICON_INFORMATION;
230 }
231
232protected:
233 // for the platforms not supporting separate main and extended messages
234 // this function should be used to combine both of them in a single string
235 wxString GetFullMessage() const
236 {
237 wxString msg = m_message;
238 if ( !m_extendedMessage.empty() )
239 msg << "\n\n" << m_extendedMessage;
240
241 return msg;
242 }
243
244 wxString m_message,
245 m_extendedMessage,
246 m_caption;
247 long m_dialogStyle;
248
23e00c55
VZ
249 // this function is called by our public SetXXXLabels() and should assign
250 // the value to var with possibly some transformation (e.g. Cocoa version
e08931c0
VZ
251 // currently uses this to remove any accelerators from the button strings
252 // while GTK+ one handles stock items specifically here)
253 virtual void DoSetCustomLabel(wxString& var, const ButtonLabel& label)
23e00c55 254 {
e08931c0 255 var = label.GetAsString();
23e00c55
VZ
256 }
257
d20ba5f8
VZ
258 // these functions return the custom label or empty string and should be
259 // used only in specific circumstances such as creating the buttons with
260 // these labels (in which case it makes sense to only use a custom label if
261 // it was really given and fall back on stock label otherwise), use the
262 // Get{Yes,No,OK,Cancel}Label() methods above otherwise
263 const wxString& GetCustomYesLabel() const { return m_yes; }
264 const wxString& GetCustomNoLabel() const { return m_no; }
265 const wxString& GetCustomOKLabel() const { return m_ok; }
7112cdd1 266 const wxString& GetCustomHelpLabel() const { return m_help; }
d20ba5f8
VZ
267 const wxString& GetCustomCancelLabel() const { return m_cancel; }
268
e08931c0 269private:
92763588
VZ
270 // these functions may be overridden to provide different defaults for the
271 // default button labels (this is used by wxGTK)
b91d65b1
MB
272 virtual wxString GetDefaultYesLabel() const { return wxGetTranslation("Yes"); }
273 virtual wxString GetDefaultNoLabel() const { return wxGetTranslation("No"); }
274 virtual wxString GetDefaultOKLabel() const { return wxGetTranslation("OK"); }
275 virtual wxString GetDefaultCancelLabel() const { return wxGetTranslation("Cancel"); }
7112cdd1 276 virtual wxString GetDefaultHelpLabel() const { return wxGetTranslation("Help"); }
92763588 277
23e00c55
VZ
278 // labels for the buttons, initially empty meaning that the defaults should
279 // be used, use GetYes/No/OK/CancelLabel() to access them
280 wxString m_yes,
281 m_no,
282 m_ok,
7112cdd1
VZ
283 m_cancel,
284 m_help;
23e00c55 285
ede7b017 286 wxDECLARE_NO_COPY_CLASS(wxMessageDialogBase);
23e00c55
VZ
287};
288
ede7b017 289#include "wx/generic/msgdlgg.h"
23e00c55 290
2afb9e16
VZ
291#if defined(__WX_COMPILING_MSGDLGG_CPP__) || \
292 defined(__WXUNIVERSAL__) || defined(__WXGPE__) || \
2afb9e16 293 (defined(__WXGTK__) && !defined(__WXGTK20__))
2afb9e16
VZ
294
295 #define wxMessageDialog wxGenericMessageDialog
e9871aaf 296#elif defined(__WXCOCOA__)
2c6dbc21 297 #include "wx/cocoa/msgdlg.h"
21709999 298#elif defined(__WXMSW__)
2afb9e16 299 #include "wx/msw/msgdlg.h"
2049ba38 300#elif defined(__WXMOTIF__)
2afb9e16 301 #include "wx/motif/msgdlg.h"
1be7a35c 302#elif defined(__WXGTK20__)
2afb9e16 303 #include "wx/gtk/msgdlg.h"
34138703 304#elif defined(__WXMAC__)
ef0e9220 305 #include "wx/osx/msgdlg.h"
1777b9bb 306#elif defined(__WXPM__)
2afb9e16 307 #include "wx/os2/msgdlg.h"
c801d85f
KB
308#endif
309
e421922f
VZ
310// ----------------------------------------------------------------------------
311// wxMessageBox: the simplest way to use wxMessageDialog
312// ----------------------------------------------------------------------------
313
53a2db12 314int WXDLLIMPEXP_CORE wxMessageBox(const wxString& message,
e5b50758
WS
315 const wxString& caption = wxMessageBoxCaptionStr,
316 long style = wxOK | wxCENTRE,
317 wxWindow *parent = NULL,
318 int x = wxDefaultCoord, int y = wxDefaultCoord);
e421922f
VZ
319
320#endif // wxUSE_MSGDLG
321
2afb9e16 322#endif // _WX_MSGDLG_H_BASE_