1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: common header and base class for wxMessageDialog
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_MSGDLG_H_BASE_
13 #define _WX_MSGDLG_H_BASE_
19 #include "wx/dialog.h"
20 #include "wx/stockitem.h"
22 extern WXDLLIMPEXP_DATA_CORE(const char) wxMessageBoxCaptionStr
[];
24 // ----------------------------------------------------------------------------
25 // wxMessageDialogBase: base class defining wxMessageDialog interface
26 // ----------------------------------------------------------------------------
28 class WXDLLIMPEXP_CORE wxMessageDialogBase
: public wxDialog
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
36 // ctors are not explicit, objects of this class can be implicitly
37 // constructed from either stock ids or strings
38 ButtonLabel(int stockId
)
41 wxASSERT_MSG( wxIsStockID(stockId
), "invalid stock id" );
44 ButtonLabel(const wxString
& label
)
45 : m_label(label
), m_stockId(wxID_NONE
)
49 ButtonLabel(const char *label
)
50 : m_label(label
), m_stockId(wxID_NONE
)
54 ButtonLabel(const wchar_t *label
)
55 : m_label(label
), m_stockId(wxID_NONE
)
59 ButtonLabel(const wxCStrData
& label
)
60 : m_label(label
), m_stockId(wxID_NONE
)
64 // default copy ctor and dtor are ok
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
71 return m_stockId
== wxID_NONE
73 : wxGetStockLabel(m_stockId
, wxSTOCK_FOR_BUTTON
);
76 // return the stock id or wxID_NONE if this is not a stock label
77 int GetStockId() const { return m_stockId
; }
80 // the label if explicitly given or empty if this is a stock item
81 const wxString m_label
;
83 // the stock item id or wxID_NONE if m_label should be used
88 wxMessageDialogBase() { m_dialogStyle
= 0; }
89 wxMessageDialogBase(wxWindow
*parent
,
90 const wxString
& message
,
91 const wxString
& caption
,
97 SetMessageDialogStyle(style
);
100 // virtual dtor for the base class
101 virtual ~wxMessageDialogBase() { }
103 wxString
GetCaption() const { return m_caption
; }
105 virtual void SetMessage(const wxString
& message
)
110 wxString
GetMessage() const { return m_message
; }
112 void SetExtendedMessage(const wxString
& extendedMessage
)
114 m_extendedMessage
= extendedMessage
;
117 wxString
GetExtendedMessage() const { return m_extendedMessage
; }
119 // change the dialog style flag
120 void SetMessageDialogStyle(long style
)
122 wxASSERT_MSG( ((style
& wxYES_NO
) == wxYES_NO
) || !(style
& wxYES_NO
),
123 "wxYES and wxNO may only be used together" );
125 wxASSERT_MSG( !(style
& wxYES
) || !(style
& wxOK
),
126 "wxOK and wxYES/wxNO can't be used together" );
128 wxASSERT_MSG( (style
& wxYES
) || (style
& wxOK
),
129 "one of wxOK and wxYES/wxNO must be used" );
131 wxASSERT_MSG( (style
& wxID_OK
) != wxID_OK
,
132 "wxMessageBox: Did you mean wxOK (and not wxID_OK)?" );
134 wxASSERT_MSG( !(style
& wxNO_DEFAULT
) || (style
& wxNO
),
135 "wxNO_DEFAULT is invalid without wxNO" );
137 wxASSERT_MSG( !(style
& wxCANCEL_DEFAULT
) || (style
& wxCANCEL
),
138 "wxCANCEL_DEFAULT is invalid without wxCANCEL" );
140 wxASSERT_MSG( !(style
& wxCANCEL_DEFAULT
) || !(style
& wxNO_DEFAULT
),
141 "only one default button can be specified" );
143 m_dialogStyle
= style
;
146 long GetMessageDialogStyle() const { return m_dialogStyle
; }
148 // customization of the message box buttons
149 virtual bool SetYesNoLabels(const ButtonLabel
& yes
,const ButtonLabel
& no
)
151 DoSetCustomLabel(m_yes
, yes
);
152 DoSetCustomLabel(m_no
, no
);
156 virtual bool SetYesNoCancelLabels(const ButtonLabel
& yes
,
157 const ButtonLabel
& no
,
158 const ButtonLabel
& cancel
)
160 DoSetCustomLabel(m_yes
, yes
);
161 DoSetCustomLabel(m_no
, no
);
162 DoSetCustomLabel(m_cancel
, cancel
);
166 virtual bool SetOKLabel(const ButtonLabel
& ok
)
168 DoSetCustomLabel(m_ok
, ok
);
172 virtual bool SetOKCancelLabels(const ButtonLabel
& ok
,
173 const ButtonLabel
& cancel
)
175 DoSetCustomLabel(m_ok
, ok
);
176 DoSetCustomLabel(m_cancel
, cancel
);
180 virtual bool SetHelpLabel(const ButtonLabel
& help
)
182 DoSetCustomLabel(m_help
, help
);
186 // test if any custom labels were set
187 bool HasCustomLabels() const
189 return !(m_ok
.empty() && m_cancel
.empty() && m_help
.empty() &&
190 m_yes
.empty() && m_no
.empty());
193 // these functions return the label to be used for the button which is
194 // either a custom label explicitly set by the user or the default label,
195 // i.e. they always return a valid string
196 wxString
GetYesLabel() const
197 { return m_yes
.empty() ? GetDefaultYesLabel() : m_yes
; }
198 wxString
GetNoLabel() const
199 { return m_no
.empty() ? GetDefaultNoLabel() : m_no
; }
200 wxString
GetOKLabel() const
201 { return m_ok
.empty() ? GetDefaultOKLabel() : m_ok
; }
202 wxString
GetCancelLabel() const
203 { return m_cancel
.empty() ? GetDefaultCancelLabel() : m_cancel
; }
204 wxString
GetHelpLabel() const
205 { return m_help
.empty() ? GetDefaultHelpLabel() : m_help
; }
207 // based on message dialog style, returns exactly one of: wxICON_NONE,
208 // wxICON_ERROR, wxICON_WARNING, wxICON_QUESTION, wxICON_INFORMATION
209 long GetEffectiveIcon() const
211 if ( m_dialogStyle
& wxICON_NONE
)
213 else if ( m_dialogStyle
& wxICON_ERROR
)
215 else if ( m_dialogStyle
& wxICON_WARNING
)
216 return wxICON_WARNING
;
217 else if ( m_dialogStyle
& wxICON_QUESTION
)
218 return wxICON_QUESTION
;
219 else if ( m_dialogStyle
& wxICON_INFORMATION
)
220 return wxICON_INFORMATION
;
221 else if ( m_dialogStyle
& wxYES
)
222 return wxICON_QUESTION
;
224 return wxICON_INFORMATION
;
228 // for the platforms not supporting separate main and extended messages
229 // this function should be used to combine both of them in a single string
230 wxString
GetFullMessage() const
232 wxString msg
= m_message
;
233 if ( !m_extendedMessage
.empty() )
234 msg
<< "\n\n" << m_extendedMessage
;
244 // this function is called by our public SetXXXLabels() and should assign
245 // the value to var with possibly some transformation (e.g. Cocoa version
246 // currently uses this to remove any accelerators from the button strings
247 // while GTK+ one handles stock items specifically here)
248 virtual void DoSetCustomLabel(wxString
& var
, const ButtonLabel
& label
)
250 var
= label
.GetAsString();
253 // these functions return the custom label or empty string and should be
254 // used only in specific circumstances such as creating the buttons with
255 // these labels (in which case it makes sense to only use a custom label if
256 // it was really given and fall back on stock label otherwise), use the
257 // Get{Yes,No,OK,Cancel}Label() methods above otherwise
258 const wxString
& GetCustomYesLabel() const { return m_yes
; }
259 const wxString
& GetCustomNoLabel() const { return m_no
; }
260 const wxString
& GetCustomOKLabel() const { return m_ok
; }
261 const wxString
& GetCustomHelpLabel() const { return m_help
; }
262 const wxString
& GetCustomCancelLabel() const { return m_cancel
; }
265 // these functions may be overridden to provide different defaults for the
266 // default button labels (this is used by wxGTK)
267 virtual wxString
GetDefaultYesLabel() const { return wxGetTranslation("Yes"); }
268 virtual wxString
GetDefaultNoLabel() const { return wxGetTranslation("No"); }
269 virtual wxString
GetDefaultOKLabel() const { return wxGetTranslation("OK"); }
270 virtual wxString
GetDefaultCancelLabel() const { return wxGetTranslation("Cancel"); }
271 virtual wxString
GetDefaultHelpLabel() const { return wxGetTranslation("Help"); }
273 // labels for the buttons, initially empty meaning that the defaults should
274 // be used, use GetYes/No/OK/CancelLabel() to access them
281 wxDECLARE_NO_COPY_CLASS(wxMessageDialogBase
);
284 #include "wx/generic/msgdlgg.h"
286 #if defined(__WX_COMPILING_MSGDLGG_CPP__) || \
287 defined(__WXUNIVERSAL__) || defined(__WXGPE__) || \
288 (defined(__WXGTK__) && !defined(__WXGTK20__))
290 #define wxMessageDialog wxGenericMessageDialog
291 #elif defined(__WXCOCOA__)
292 #include "wx/cocoa/msgdlg.h"
293 #elif defined(__WXMSW__)
294 #include "wx/msw/msgdlg.h"
295 #elif defined(__WXMOTIF__)
296 #include "wx/motif/msgdlg.h"
297 #elif defined(__WXGTK20__)
298 #include "wx/gtk/msgdlg.h"
299 #elif defined(__WXMAC__)
300 #include "wx/osx/msgdlg.h"
301 #elif defined(__WXPM__)
302 #include "wx/os2/msgdlg.h"
305 // ----------------------------------------------------------------------------
306 // wxMessageBox: the simplest way to use wxMessageDialog
307 // ----------------------------------------------------------------------------
309 int WXDLLIMPEXP_CORE
wxMessageBox(const wxString
& message
,
310 const wxString
& caption
= wxMessageBoxCaptionStr
,
311 long style
= wxOK
| wxCENTRE
,
312 wxWindow
*parent
= NULL
,
313 int x
= wxDefaultCoord
, int y
= wxDefaultCoord
);
315 #endif // wxUSE_MSGDLG
317 #endif // _WX_MSGDLG_H_BASE_