]>
Commit | Line | Data |
---|---|---|
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 | // 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 | ||
103 | wxString GetCaption() const { return m_caption; } | |
104 | ||
105 | virtual void SetMessage(const wxString& message) | |
106 | { | |
107 | m_message = message; | |
108 | } | |
109 | ||
110 | wxString GetMessage() const { return m_message; } | |
111 | ||
112 | void SetExtendedMessage(const wxString& extendedMessage) | |
113 | { | |
114 | m_extendedMessage = extendedMessage; | |
115 | } | |
116 | ||
117 | wxString GetExtendedMessage() const { return m_extendedMessage; } | |
118 | ||
119 | // change the dialog style flag | |
120 | void SetMessageDialogStyle(long style) | |
121 | { | |
122 | wxASSERT_MSG( ((style & wxYES_NO) == wxYES_NO) || !(style & wxYES_NO), | |
123 | "wxYES and wxNO may only be used together" ); | |
124 | ||
125 | wxASSERT_MSG( !(style & wxYES) || !(style & wxOK), | |
126 | "wxOK and wxYES/wxNO can't be used together" ); | |
127 | ||
128 | wxASSERT_MSG( (style & wxYES) || (style & wxOK), | |
129 | "one of wxOK and wxYES/wxNO must be used" ); | |
130 | ||
131 | wxASSERT_MSG( (style & wxID_OK) != wxID_OK, | |
132 | "wxMessageBox: Did you mean wxOK (and not wxID_OK)?" ); | |
133 | ||
134 | wxASSERT_MSG( !(style & wxNO_DEFAULT) || (style & wxNO), | |
135 | "wxNO_DEFAULT is invalid without wxNO" ); | |
136 | ||
137 | wxASSERT_MSG( !(style & wxCANCEL_DEFAULT) || (style & wxCANCEL), | |
138 | "wxCANCEL_DEFAULT is invalid without wxCANCEL" ); | |
139 | ||
140 | wxASSERT_MSG( !(style & wxCANCEL_DEFAULT) || !(style & wxNO_DEFAULT), | |
141 | "only one default button can be specified" ); | |
142 | ||
143 | m_dialogStyle = style; | |
144 | } | |
145 | ||
146 | long GetMessageDialogStyle() const { return m_dialogStyle; } | |
147 | ||
148 | // customization of the message box buttons | |
149 | virtual bool SetYesNoLabels(const ButtonLabel& yes,const ButtonLabel& no) | |
150 | { | |
151 | DoSetCustomLabel(m_yes, yes); | |
152 | DoSetCustomLabel(m_no, no); | |
153 | return true; | |
154 | } | |
155 | ||
156 | virtual bool SetYesNoCancelLabels(const ButtonLabel& yes, | |
157 | const ButtonLabel& no, | |
158 | const ButtonLabel& cancel) | |
159 | { | |
160 | DoSetCustomLabel(m_yes, yes); | |
161 | DoSetCustomLabel(m_no, no); | |
162 | DoSetCustomLabel(m_cancel, cancel); | |
163 | return true; | |
164 | } | |
165 | ||
166 | virtual bool SetOKLabel(const ButtonLabel& ok) | |
167 | { | |
168 | DoSetCustomLabel(m_ok, ok); | |
169 | return true; | |
170 | } | |
171 | ||
172 | virtual bool SetOKCancelLabels(const ButtonLabel& ok, | |
173 | const ButtonLabel& cancel) | |
174 | { | |
175 | DoSetCustomLabel(m_ok, ok); | |
176 | DoSetCustomLabel(m_cancel, cancel); | |
177 | return true; | |
178 | } | |
179 | ||
180 | // test if any custom labels were set | |
181 | bool HasCustomLabels() const | |
182 | { | |
183 | return !(m_ok.empty() && m_cancel.empty() && | |
184 | m_yes.empty() && m_no.empty()); | |
185 | } | |
186 | ||
187 | // these functions return the label to be used for the button which is | |
188 | // either a custom label explicitly set by the user or the default label, | |
189 | // i.e. they always return a valid string | |
190 | wxString GetYesLabel() const | |
191 | { return m_yes.empty() ? GetDefaultYesLabel() : m_yes; } | |
192 | wxString GetNoLabel() const | |
193 | { return m_no.empty() ? GetDefaultNoLabel() : m_no; } | |
194 | wxString GetOKLabel() const | |
195 | { return m_ok.empty() ? GetDefaultOKLabel() : m_ok; } | |
196 | wxString GetCancelLabel() const | |
197 | { return m_cancel.empty() ? GetDefaultCancelLabel() : m_cancel; } | |
198 | ||
199 | // based on message dialog style, returns exactly one of: wxICON_NONE, | |
200 | // wxICON_ERROR, wxICON_WARNING, wxICON_QUESTION, wxICON_INFORMATION | |
201 | long GetEffectiveIcon() const | |
202 | { | |
203 | if ( m_dialogStyle & wxICON_NONE ) | |
204 | return wxICON_NONE; | |
205 | else if ( m_dialogStyle & wxICON_ERROR ) | |
206 | return wxICON_ERROR; | |
207 | else if ( m_dialogStyle & wxICON_WARNING ) | |
208 | return wxICON_WARNING; | |
209 | else if ( m_dialogStyle & wxICON_QUESTION ) | |
210 | return wxICON_QUESTION; | |
211 | else if ( m_dialogStyle & wxICON_INFORMATION ) | |
212 | return wxICON_INFORMATION; | |
213 | else if ( m_dialogStyle & wxYES ) | |
214 | return wxICON_QUESTION; | |
215 | else | |
216 | return wxICON_INFORMATION; | |
217 | } | |
218 | ||
219 | protected: | |
220 | // for the platforms not supporting separate main and extended messages | |
221 | // this function should be used to combine both of them in a single string | |
222 | wxString GetFullMessage() const | |
223 | { | |
224 | wxString msg = m_message; | |
225 | if ( !m_extendedMessage.empty() ) | |
226 | msg << "\n\n" << m_extendedMessage; | |
227 | ||
228 | return msg; | |
229 | } | |
230 | ||
231 | wxString m_message, | |
232 | m_extendedMessage, | |
233 | m_caption; | |
234 | long m_dialogStyle; | |
235 | ||
236 | // this function is called by our public SetXXXLabels() and should assign | |
237 | // the value to var with possibly some transformation (e.g. Cocoa version | |
238 | // currently uses this to remove any accelerators from the button strings | |
239 | // while GTK+ one handles stock items specifically here) | |
240 | virtual void DoSetCustomLabel(wxString& var, const ButtonLabel& label) | |
241 | { | |
242 | var = label.GetAsString(); | |
243 | } | |
244 | ||
245 | // these functions return the custom label or empty string and should be | |
246 | // used only in specific circumstances such as creating the buttons with | |
247 | // these labels (in which case it makes sense to only use a custom label if | |
248 | // it was really given and fall back on stock label otherwise), use the | |
249 | // Get{Yes,No,OK,Cancel}Label() methods above otherwise | |
250 | const wxString& GetCustomYesLabel() const { return m_yes; } | |
251 | const wxString& GetCustomNoLabel() const { return m_no; } | |
252 | const wxString& GetCustomOKLabel() const { return m_ok; } | |
253 | const wxString& GetCustomCancelLabel() const { return m_cancel; } | |
254 | ||
255 | private: | |
256 | // these functions may be overridden to provide different defaults for the | |
257 | // default button labels (this is used by wxGTK) | |
258 | virtual wxString GetDefaultYesLabel() const { return wxGetTranslation("Yes"); } | |
259 | virtual wxString GetDefaultNoLabel() const { return wxGetTranslation("No"); } | |
260 | virtual wxString GetDefaultOKLabel() const { return wxGetTranslation("OK"); } | |
261 | virtual wxString GetDefaultCancelLabel() const { return wxGetTranslation("Cancel"); } | |
262 | ||
263 | // labels for the buttons, initially empty meaning that the defaults should | |
264 | // be used, use GetYes/No/OK/CancelLabel() to access them | |
265 | wxString m_yes, | |
266 | m_no, | |
267 | m_ok, | |
268 | m_cancel; | |
269 | ||
270 | wxDECLARE_NO_COPY_CLASS(wxMessageDialogBase); | |
271 | }; | |
272 | ||
273 | #include "wx/generic/msgdlgg.h" | |
274 | ||
275 | #if defined(__WX_COMPILING_MSGDLGG_CPP__) || \ | |
276 | defined(__WXUNIVERSAL__) || defined(__WXGPE__) || \ | |
277 | (defined(__WXGTK__) && !defined(__WXGTK20__)) | |
278 | ||
279 | #define wxMessageDialog wxGenericMessageDialog | |
280 | #elif defined(__WXCOCOA__) | |
281 | #include "wx/cocoa/msgdlg.h" | |
282 | #elif defined(__WXPALMOS__) | |
283 | #include "wx/palmos/msgdlg.h" | |
284 | #elif defined(__WXMSW__) | |
285 | #include "wx/msw/msgdlg.h" | |
286 | #elif defined(__WXMOTIF__) | |
287 | #include "wx/motif/msgdlg.h" | |
288 | #elif defined(__WXGTK20__) | |
289 | #include "wx/gtk/msgdlg.h" | |
290 | #elif defined(__WXMAC__) | |
291 | #include "wx/osx/msgdlg.h" | |
292 | #elif defined(__WXPM__) | |
293 | #include "wx/os2/msgdlg.h" | |
294 | #endif | |
295 | ||
296 | // ---------------------------------------------------------------------------- | |
297 | // wxMessageBox: the simplest way to use wxMessageDialog | |
298 | // ---------------------------------------------------------------------------- | |
299 | ||
300 | int WXDLLIMPEXP_CORE wxMessageBox(const wxString& message, | |
301 | const wxString& caption = wxMessageBoxCaptionStr, | |
302 | long style = wxOK | wxCENTRE, | |
303 | wxWindow *parent = NULL, | |
304 | int x = wxDefaultCoord, int y = wxDefaultCoord); | |
305 | ||
306 | #endif // wxUSE_MSGDLG | |
307 | ||
308 | #endif // _WX_MSGDLG_H_BASE_ |