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