]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/gtk/msgdlg.cpp | |
3 | // Purpose: wxMessageDialog for GTK+2 | |
4 | // Author: Vaclav Slavik | |
5 | // Modified by: | |
6 | // Created: 2003/02/28 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Vaclav Slavik, 2003 | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // For compilers that support precompilation, includes "wx.h". | |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
16 | #pragma hdrstop | |
17 | #endif | |
18 | ||
19 | #if wxUSE_MSGDLG && !defined(__WXGPE__) | |
20 | ||
21 | #include "wx/msgdlg.h" | |
22 | ||
23 | #ifndef WX_PRECOMP | |
24 | #include "wx/intl.h" | |
25 | #endif | |
26 | ||
27 | #include "wx/gtk/private.h" | |
28 | #include "wx/gtk/private/mnemonics.h" | |
29 | #include <gtk/gtk.h> | |
30 | ||
31 | #if wxUSE_LIBHILDON | |
32 | #include <hildon-widgets/hildon-note.h> | |
33 | #endif // wxUSE_LIBHILDON | |
34 | ||
35 | IMPLEMENT_CLASS(wxMessageDialog, wxDialog) | |
36 | ||
37 | wxMessageDialog::wxMessageDialog(wxWindow *parent, | |
38 | const wxString& message, | |
39 | const wxString& caption, | |
40 | long style, | |
41 | const wxPoint& WXUNUSED(pos)) | |
42 | : wxMessageDialogWithCustomLabels(GetParentForModalDialog(parent), | |
43 | message, | |
44 | caption, | |
45 | style) | |
46 | { | |
47 | } | |
48 | ||
49 | wxString wxMessageDialog::GetDefaultYesLabel() const | |
50 | { | |
51 | return GTK_STOCK_YES; | |
52 | } | |
53 | ||
54 | wxString wxMessageDialog::GetDefaultNoLabel() const | |
55 | { | |
56 | return GTK_STOCK_NO; | |
57 | } | |
58 | ||
59 | wxString wxMessageDialog::GetDefaultOKLabel() const | |
60 | { | |
61 | return GTK_STOCK_OK; | |
62 | } | |
63 | ||
64 | wxString wxMessageDialog::GetDefaultCancelLabel() const | |
65 | { | |
66 | return GTK_STOCK_CANCEL; | |
67 | } | |
68 | ||
69 | void wxMessageDialog::DoSetCustomLabel(wxString& var, const ButtonLabel& label) | |
70 | { | |
71 | int stockId = label.GetStockId(); | |
72 | if ( stockId == wxID_NONE ) | |
73 | { | |
74 | wxMessageDialogWithCustomLabels::DoSetCustomLabel(var, label); | |
75 | var = wxConvertMnemonicsToGTK(var); | |
76 | } | |
77 | else // stock label | |
78 | { | |
79 | var = wxGetStockGtkID(stockId); | |
80 | } | |
81 | } | |
82 | ||
83 | void wxMessageDialog::GTKCreateMsgDialog() | |
84 | { | |
85 | GtkWindow * const parent = m_parent ? GTK_WINDOW(m_parent->m_widget) : NULL; | |
86 | ||
87 | #if wxUSE_LIBHILDON | |
88 | const char *stockIcon; | |
89 | if ( m_dialogStyle & wxICON_NONE ) | |
90 | stockIcon = ""; | |
91 | else if ( m_dialogStyle & wxICON_ERROR ) | |
92 | stockIcon = "qgn_note_gene_syserror"; | |
93 | else if ( m_dialogStyle & wxICON_EXCLAMATION ) | |
94 | stockIcon = "qgn_note_gene_syswarning"; | |
95 | else if ( m_dialogStyle & wxICON_INFORMATION ) | |
96 | stockIcon = "qgn_note_info"; | |
97 | else if ( m_dialogStyle & wxICON_QUESTION ) | |
98 | stockIcon = "qgn_note_confirm"; | |
99 | else | |
100 | stockIcon = ""; | |
101 | ||
102 | // there is no generic note creation function in public API so we have no | |
103 | // choice but to use g_object_new() directly | |
104 | m_widget = (GtkWidget *)g_object_new | |
105 | ( | |
106 | HILDON_TYPE_NOTE, | |
107 | "note_type", HILDON_NOTE_CONFIRMATION_BUTTON_TYPE, | |
108 | "description", (const char *)GetFullMessage().utf8_str(), | |
109 | "icon", stockIcon, | |
110 | NULL | |
111 | ); | |
112 | #else // !wxUSE_LIBHILDON | |
113 | GtkMessageType type = GTK_MESSAGE_ERROR; | |
114 | GtkButtonsType buttons = GTK_BUTTONS_NONE; | |
115 | ||
116 | // when using custom labels, we have to add all the buttons ourselves | |
117 | if ( !HasCustomLabels() ) | |
118 | { | |
119 | if ( m_dialogStyle & wxYES_NO ) | |
120 | { | |
121 | if ( !(m_dialogStyle & wxCANCEL) ) | |
122 | buttons = GTK_BUTTONS_YES_NO; | |
123 | //else: no standard GTK_BUTTONS_YES_NO_CANCEL so leave as NONE | |
124 | } | |
125 | else if ( m_dialogStyle & wxOK ) | |
126 | { | |
127 | buttons = m_dialogStyle & wxCANCEL ? GTK_BUTTONS_OK_CANCEL | |
128 | : GTK_BUTTONS_OK; | |
129 | } | |
130 | } | |
131 | ||
132 | #ifdef __WXGTK210__ | |
133 | if ( gtk_check_version(2, 10, 0) == NULL && (m_dialogStyle & wxICON_NONE)) | |
134 | type = GTK_MESSAGE_OTHER; | |
135 | else | |
136 | #endif // __WXGTK210__ | |
137 | if (m_dialogStyle & wxICON_EXCLAMATION) | |
138 | type = GTK_MESSAGE_WARNING; | |
139 | else if (m_dialogStyle & wxICON_ERROR) | |
140 | type = GTK_MESSAGE_ERROR; | |
141 | else if (m_dialogStyle & wxICON_INFORMATION) | |
142 | type = GTK_MESSAGE_INFO; | |
143 | else if (m_dialogStyle & wxICON_QUESTION) | |
144 | type = GTK_MESSAGE_QUESTION; | |
145 | else | |
146 | { | |
147 | // if no style is explicitly specified, detect the suitable icon | |
148 | // ourselves (this can be disabled by using wxICON_NONE) | |
149 | type = m_dialogStyle & wxYES ? GTK_MESSAGE_QUESTION : GTK_MESSAGE_INFO; | |
150 | } | |
151 | ||
152 | wxString message; | |
153 | #if GTK_CHECK_VERSION(2, 6, 0) | |
154 | bool needsExtMessage = false; | |
155 | if ( gtk_check_version(2, 6, 0) == NULL && !m_extendedMessage.empty() ) | |
156 | { | |
157 | message = m_message; | |
158 | needsExtMessage = true; | |
159 | } | |
160 | else // extended message not needed or not supported | |
161 | #endif // GTK+ 2.6+ | |
162 | { | |
163 | message = GetFullMessage(); | |
164 | } | |
165 | ||
166 | m_widget = gtk_message_dialog_new(parent, | |
167 | GTK_DIALOG_MODAL, | |
168 | type, | |
169 | buttons, | |
170 | "%s", | |
171 | (const char*)wxGTK_CONV(message)); | |
172 | ||
173 | #if GTK_CHECK_VERSION(2, 6, 0) | |
174 | if ( needsExtMessage ) | |
175 | { | |
176 | gtk_message_dialog_format_secondary_text | |
177 | ( | |
178 | (GtkMessageDialog *)m_widget, | |
179 | "%s", | |
180 | (const char *)wxGTK_CONV(m_extendedMessage) | |
181 | ); | |
182 | } | |
183 | #endif // GTK+ 2.6+ | |
184 | #endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON | |
185 | ||
186 | g_object_ref(m_widget); | |
187 | ||
188 | if (m_caption != wxMessageBoxCaptionStr) | |
189 | gtk_window_set_title(GTK_WINDOW(m_widget), wxGTK_CONV(m_caption)); | |
190 | ||
191 | GtkDialog * const dlg = GTK_DIALOG(m_widget); | |
192 | ||
193 | // we need to add buttons manually if we use custom labels or always for | |
194 | // Yes/No/Cancel dialog as GTK+ doesn't support it natively and when using | |
195 | // Hildon we add all the buttons manually as it doesn't support too many of | |
196 | // the combinations we may have | |
197 | #if wxUSE_LIBHILDON | |
198 | static const bool addButtons = true; | |
199 | #else // !wxUSE_LIBHILDON | |
200 | const bool addButtons = buttons == GTK_BUTTONS_NONE; | |
201 | #endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON | |
202 | ||
203 | if ( m_dialogStyle & wxYES_NO ) // Yes/No or Yes/No/Cancel dialog | |
204 | { | |
205 | if ( addButtons ) | |
206 | { | |
207 | gtk_dialog_add_button(dlg, wxGTK_CONV(GetNoLabel()), | |
208 | GTK_RESPONSE_NO); | |
209 | gtk_dialog_add_button(dlg, wxGTK_CONV(GetYesLabel()), | |
210 | GTK_RESPONSE_YES); | |
211 | ||
212 | if ( m_dialogStyle & wxCANCEL ) | |
213 | { | |
214 | gtk_dialog_add_button(dlg, wxGTK_CONV(GetCancelLabel()), | |
215 | GTK_RESPONSE_CANCEL); | |
216 | } | |
217 | } | |
218 | ||
219 | // it'd probably be harmless to call gtk_dialog_set_default_response() | |
220 | // twice but why do it if we're going to change the default below | |
221 | // anyhow | |
222 | if ( !(m_dialogStyle & wxCANCEL_DEFAULT) ) | |
223 | { | |
224 | gtk_dialog_set_default_response(dlg, | |
225 | m_dialogStyle & wxNO_DEFAULT | |
226 | ? GTK_RESPONSE_NO | |
227 | : GTK_RESPONSE_YES); | |
228 | } | |
229 | } | |
230 | else if ( addButtons ) // Ok or Ok/Cancel dialog | |
231 | { | |
232 | gtk_dialog_add_button(dlg, wxGTK_CONV(GetOKLabel()), GTK_RESPONSE_OK); | |
233 | if ( m_dialogStyle & wxCANCEL ) | |
234 | { | |
235 | gtk_dialog_add_button(dlg, wxGTK_CONV(GetCancelLabel()), | |
236 | GTK_RESPONSE_CANCEL); | |
237 | } | |
238 | } | |
239 | ||
240 | if ( m_dialogStyle & wxCANCEL_DEFAULT ) | |
241 | { | |
242 | gtk_dialog_set_default_response(dlg, GTK_RESPONSE_CANCEL); | |
243 | } | |
244 | } | |
245 | ||
246 | int wxMessageDialog::ShowModal() | |
247 | { | |
248 | // break the mouse capture as it would interfere with modal dialog (see | |
249 | // wxDialog::ShowModal) | |
250 | wxWindow * const win = wxWindow::GetCapture(); | |
251 | if ( win ) | |
252 | win->GTKReleaseMouseAndNotify(); | |
253 | ||
254 | if ( !m_widget ) | |
255 | { | |
256 | GTKCreateMsgDialog(); | |
257 | wxCHECK_MSG( m_widget, wxID_CANCEL, | |
258 | wxT("failed to create GtkMessageDialog") ); | |
259 | } | |
260 | ||
261 | // This should be necessary, but otherwise the | |
262 | // parent TLW will disappear.. | |
263 | if (m_parent) | |
264 | gtk_window_present( GTK_WINDOW(m_parent->m_widget) ); | |
265 | ||
266 | gint result = gtk_dialog_run(GTK_DIALOG(m_widget)); | |
267 | gtk_widget_destroy(m_widget); | |
268 | g_object_unref(m_widget); | |
269 | m_widget = NULL; | |
270 | ||
271 | switch (result) | |
272 | { | |
273 | default: | |
274 | wxFAIL_MSG(wxT("unexpected GtkMessageDialog return code")); | |
275 | // fall through | |
276 | ||
277 | case GTK_RESPONSE_CANCEL: | |
278 | case GTK_RESPONSE_DELETE_EVENT: | |
279 | case GTK_RESPONSE_CLOSE: | |
280 | return wxID_CANCEL; | |
281 | case GTK_RESPONSE_OK: | |
282 | return wxID_OK; | |
283 | case GTK_RESPONSE_YES: | |
284 | return wxID_YES; | |
285 | case GTK_RESPONSE_NO: | |
286 | return wxID_NO; | |
287 | } | |
288 | } | |
289 | ||
290 | ||
291 | #endif // wxUSE_MSGDLG && !defined(__WXGPE__) |