]> git.saurik.com Git - wxWidgets.git/blob - include/wx/msgdlg.h
make it possible to use stock ids for custom message box labels
[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 // default copy ctor and dtor are ok
50
51 // get the string label, whether it was originally specified directly
52 // or as a stock id -- this is only useful for platforms without native
53 // stock items id support
54 wxString GetAsString() const
55 {
56 return m_stockId == wxID_NONE ? m_label
57 : wxGetStockLabel(m_stockId);
58 }
59
60 // return the stock id or wxID_NONE if this is not a stock label
61 int GetStockId() const { return m_stockId; }
62
63 private:
64 // the label if explicitly given or empty if this is a stock item
65 const wxString m_label;
66
67 // the stock item id or wxID_NONE if m_label should be used
68 const int m_stockId;
69 };
70
71
72 // ctors
73 wxMessageDialogBase() { m_dialogStyle = 0; }
74 wxMessageDialogBase(wxWindow *parent,
75 const wxString& message,
76 const wxString& caption,
77 long style)
78 : m_message(message),
79 m_caption(caption)
80 {
81 m_parent = parent;
82 SetMessageDialogStyle(style);
83 }
84
85 // virtual dtor for the base class
86 virtual ~wxMessageDialogBase() { }
87
88
89 // methods for setting up more custom message dialogs -- all functions
90 // return false if they're not implemented
91 virtual bool SetYesNoLabels(const ButtonLabel& WXUNUSED(yes),
92 const ButtonLabel& WXUNUSED(no))
93 {
94 return false;
95 }
96
97 virtual bool SetYesNoCancelLabels(const ButtonLabel& WXUNUSED(yes),
98 const ButtonLabel& WXUNUSED(no),
99 const ButtonLabel& WXUNUSED(cancel))
100 {
101 return false;
102 }
103
104 virtual bool SetOKLabel(const ButtonLabel& WXUNUSED(ok))
105 {
106 return false;
107 }
108
109 virtual bool SetOKCancelLabels(const ButtonLabel& WXUNUSED(ok),
110 const ButtonLabel& WXUNUSED(cancel))
111 {
112 return false;
113 }
114
115 virtual void SetMessage(const wxString& message)
116 {
117 m_message = message;
118 }
119
120 virtual void SetExtendedMessage(const wxString& extendedMessage)
121 {
122 m_extendedMessage = extendedMessage;
123 }
124
125 protected:
126 // common validation of wxMessageDialog style
127 void SetMessageDialogStyle(long style)
128 {
129 wxASSERT_MSG( ((style & wxYES_NO) == wxYES_NO) || !(style & wxYES_NO),
130 "wxYES and wxNO may only be used together" );
131
132 wxASSERT_MSG( (style & wxID_OK) != wxID_OK,
133 "wxMessageBox: Did you mean wxOK (and not wxID_OK)?" );
134
135 m_dialogStyle = style;
136 }
137
138 long GetMessageDialogStyle() const { return m_dialogStyle; }
139
140
141 // for the platforms not supporting separate main and extended messages
142 // this function should be used to combine both of them in a single string
143 wxString GetFullMessage() const
144 {
145 wxString msg = m_message;
146 if ( !m_extendedMessage.empty() )
147 msg << "\n\n" << m_extendedMessage;
148
149 return msg;
150 }
151
152 wxString m_message,
153 m_extendedMessage,
154 m_caption;
155 long m_dialogStyle;
156
157 DECLARE_NO_COPY_CLASS(wxMessageDialogBase)
158 };
159
160 // this is a helper class for native wxMessageDialog implementations which need
161 // to store the custom button labels as member variables and then use them in
162 // ShowModal() (there could conceivably be a port which would have some native
163 // functions for setting these labels immediately and we also don't need to
164 // store them at all if custom labels are not supported, which is why we do
165 // this in a separate class and not wxMessageDialogBase itself)
166 #if defined(__WXCOCOA__) || \
167 defined(__WXGTK20__) || \
168 defined(__WXMAC__) || \
169 defined(__WXMSW__)
170
171 class WXDLLIMPEXP_CORE wxMessageDialogWithCustomLabels
172 : public wxMessageDialogBase
173 {
174 public:
175 // ctors
176 wxMessageDialogWithCustomLabels() { }
177 wxMessageDialogWithCustomLabels(wxWindow *parent,
178 const wxString& message,
179 const wxString& caption,
180 long style)
181 : wxMessageDialogBase(parent, message, caption, style)
182 {
183 }
184
185 // customization of the message box buttons
186 virtual bool SetYesNoLabels(const ButtonLabel& yes,const ButtonLabel& no)
187 {
188 DoSetCustomLabel(m_yes, yes);
189 DoSetCustomLabel(m_no, no);
190 return true;
191 }
192
193 virtual bool SetYesNoCancelLabels(const ButtonLabel& yes,
194 const ButtonLabel& no,
195 const ButtonLabel& cancel)
196 {
197 DoSetCustomLabel(m_yes, yes);
198 DoSetCustomLabel(m_no, no);
199 DoSetCustomLabel(m_cancel, cancel);
200 return true;
201 }
202
203 virtual bool SetOKLabel(const ButtonLabel& ok)
204 {
205 DoSetCustomLabel(m_ok, ok);
206 return true;
207 }
208
209 virtual bool SetOKCancelLabels(const ButtonLabel& ok,
210 const ButtonLabel& cancel)
211 {
212 DoSetCustomLabel(m_ok, ok);
213 DoSetCustomLabel(m_cancel, cancel);
214 return true;
215 }
216
217 protected:
218 // test if any custom labels were set
219 bool HasCustomLabels() const
220 {
221 return !(m_ok.empty() && m_cancel.empty() &&
222 m_yes.empty() && m_no.empty());
223 }
224
225 // these functions return the label to be used for the button which is
226 // either a custom label explicitly set by the user or the default label,
227 // i.e. they always return a valid string
228 wxString GetYesLabel() const
229 { return m_yes.empty() ? GetDefaultYesLabel() : m_yes; }
230 wxString GetNoLabel() const
231 { return m_no.empty() ? GetDefaultNoLabel() : m_no; }
232 wxString GetOKLabel() const
233 { return m_ok.empty() ? GetDefaultOKLabel() : m_ok; }
234 wxString GetCancelLabel() const
235 { return m_cancel.empty() ? GetDefaultCancelLabel() : m_cancel; }
236
237 // this function is called by our public SetXXXLabels() and should assign
238 // the value to var with possibly some transformation (e.g. Cocoa version
239 // currently uses this to remove any accelerators from the button strings
240 // while GTK+ one handles stock items specifically here)
241 virtual void DoSetCustomLabel(wxString& var, const ButtonLabel& label)
242 {
243 var = label.GetAsString();
244 }
245
246 private:
247 // these functions may be overridden to provide different defaults for the
248 // default button labels (this is used by wxGTK)
249 virtual wxString GetDefaultYesLabel() const { return _("Yes"); }
250 virtual wxString GetDefaultNoLabel() const { return _("No"); }
251 virtual wxString GetDefaultOKLabel() const { return _("OK"); }
252 virtual wxString GetDefaultCancelLabel() const { return _("Cancel"); }
253
254 // labels for the buttons, initially empty meaning that the defaults should
255 // be used, use GetYes/No/OK/CancelLabel() to access them
256 wxString m_yes,
257 m_no,
258 m_ok,
259 m_cancel;
260
261 DECLARE_NO_COPY_CLASS(wxMessageDialogWithCustomLabels)
262 };
263
264 #endif // ports needing wxMessageDialogWithCustomLabels
265
266 #if defined(__WX_COMPILING_MSGDLGG_CPP__) || \
267 defined(__WXUNIVERSAL__) || defined(__WXGPE__) || \
268 (defined(__WXGTK__) && !defined(__WXGTK20__))
269 #include "wx/generic/msgdlgg.h"
270
271 #define wxMessageDialog wxGenericMessageDialog
272 #elif defined(__WXCOCOA__)
273 #include "wx/cocoa/msgdlg.h"
274 #elif defined(__WXPALMOS__)
275 #include "wx/palmos/msgdlg.h"
276 #elif defined(__WXMSW__)
277 #include "wx/msw/msgdlg.h"
278 #elif defined(__WXMOTIF__)
279 #include "wx/motif/msgdlg.h"
280 #elif defined(__WXGTK20__)
281 #include "wx/gtk/msgdlg.h"
282 #elif defined(__WXMAC__)
283 #include "wx/osx/msgdlg.h"
284 #elif defined(__WXPM__)
285 #include "wx/os2/msgdlg.h"
286 #endif
287
288 // ----------------------------------------------------------------------------
289 // wxMessageBox: the simplest way to use wxMessageDialog
290 // ----------------------------------------------------------------------------
291
292 int WXDLLIMPEXP_CORE wxMessageBox(const wxString& message,
293 const wxString& caption = wxMessageBoxCaptionStr,
294 long style = wxOK | wxCENTRE,
295 wxWindow *parent = NULL,
296 int x = wxDefaultCoord, int y = wxDefaultCoord);
297
298 #endif // wxUSE_MSGDLG
299
300 #endif // _WX_MSGDLG_H_BASE_