]>
Commit | Line | Data |
---|---|---|
13a7abf9 | 1 | ///////////////////////////////////////////////////////////////////////////// |
e5b50758 | 2 | // Name: src/gtk/msgdlg.cpp |
13a7abf9 VS |
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 | |
e5b50758 | 9 | // Licence: wxWindows licence |
13a7abf9 VS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
13a7abf9 VS |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
88a7a4e1 | 16 | #pragma hdrstop |
13a7abf9 VS |
17 | #endif |
18 | ||
ff654490 | 19 | #if wxUSE_MSGDLG && !defined(__WXGPE__) |
13a7abf9 | 20 | |
e1bf3ad3 | 21 | #include "wx/msgdlg.h" |
88a7a4e1 WS |
22 | |
23 | #ifndef WX_PRECOMP | |
24 | #include "wx/intl.h" | |
25 | #endif | |
26 | ||
643e9cf9 VS |
27 | #include "wx/testing.h" |
28 | ||
9dc44eff | 29 | #include <gtk/gtk.h> |
13a7abf9 | 30 | #include "wx/gtk/private.h" |
ce668f29 | 31 | #include "wx/gtk/private/messagetype.h" |
92763588 | 32 | #include "wx/gtk/private/mnemonics.h" |
3c2f3a60 | 33 | #include "wx/gtk/private/dialogcount.h" |
13a7abf9 | 34 | |
c96d7bec VZ |
35 | #if wxUSE_LIBHILDON |
36 | #include <hildon-widgets/hildon-note.h> | |
37 | #endif // wxUSE_LIBHILDON | |
38 | ||
426d19f1 JS |
39 | #if wxUSE_LIBHILDON2 |
40 | #include <hildon/hildon.h> | |
41 | #endif // wxUSE_LIBHILDON2 | |
42 | ||
13a7abf9 VS |
43 | IMPLEMENT_CLASS(wxMessageDialog, wxDialog) |
44 | ||
45 | wxMessageDialog::wxMessageDialog(wxWindow *parent, | |
46 | const wxString& message, | |
47 | const wxString& caption, | |
48 | long style, | |
49 | const wxPoint& WXUNUSED(pos)) | |
ede7b017 | 50 | : wxMessageDialogBase |
cdc48273 VZ |
51 | ( |
52 | GetParentForModalDialog(parent, style), | |
53 | message, | |
54 | caption, | |
55 | style | |
56 | ) | |
13a7abf9 | 57 | { |
2afb9e16 | 58 | } |
13a7abf9 | 59 | |
92763588 VZ |
60 | wxString wxMessageDialog::GetDefaultYesLabel() const |
61 | { | |
62 | return GTK_STOCK_YES; | |
63 | } | |
64 | ||
65 | wxString wxMessageDialog::GetDefaultNoLabel() const | |
66 | { | |
67 | return GTK_STOCK_NO; | |
68 | } | |
69 | ||
70 | wxString wxMessageDialog::GetDefaultOKLabel() const | |
71 | { | |
72 | return GTK_STOCK_OK; | |
73 | } | |
74 | ||
75 | wxString wxMessageDialog::GetDefaultCancelLabel() const | |
76 | { | |
77 | return GTK_STOCK_CANCEL; | |
78 | } | |
79 | ||
7112cdd1 VZ |
80 | wxString wxMessageDialog::GetDefaultHelpLabel() const |
81 | { | |
82 | return GTK_STOCK_HELP; | |
83 | } | |
84 | ||
e08931c0 | 85 | void wxMessageDialog::DoSetCustomLabel(wxString& var, const ButtonLabel& label) |
92763588 | 86 | { |
e08931c0 VZ |
87 | int stockId = label.GetStockId(); |
88 | if ( stockId == wxID_NONE ) | |
89 | { | |
ede7b017 | 90 | wxMessageDialogBase::DoSetCustomLabel(var, label); |
e08931c0 VZ |
91 | var = wxConvertMnemonicsToGTK(var); |
92 | } | |
93 | else // stock label | |
94 | { | |
95 | var = wxGetStockGtkID(stockId); | |
96 | } | |
92763588 VZ |
97 | } |
98 | ||
2afb9e16 VZ |
99 | void wxMessageDialog::GTKCreateMsgDialog() |
100 | { | |
c96d7bec VZ |
101 | GtkWindow * const parent = m_parent ? GTK_WINDOW(m_parent->m_widget) : NULL; |
102 | ||
426d19f1 | 103 | #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2 |
a4578b0c VZ |
104 | const char *stockIcon = ""; |
105 | ||
106 | switch ( GetEffectiveIcon() ) | |
107 | { | |
108 | case wxICON_ERROR: | |
109 | stockIcon = "qgn_note_gene_syserror"; | |
110 | break; | |
111 | ||
112 | case wxICON_WARNING: | |
113 | stockIcon = "qgn_note_gene_syswarning"; | |
114 | break; | |
115 | ||
116 | case wxICON_QUESTION: | |
117 | stockIcon = "qgn_note_confirm"; | |
118 | break; | |
119 | ||
120 | case wxICON_INFORMATION: | |
121 | stockIcon = "qgn_note_info"; | |
122 | break; | |
123 | } | |
c96d7bec VZ |
124 | |
125 | // there is no generic note creation function in public API so we have no | |
126 | // choice but to use g_object_new() directly | |
127 | m_widget = (GtkWidget *)g_object_new | |
128 | ( | |
129 | HILDON_TYPE_NOTE, | |
426d19f1 | 130 | #if wxUSE_LIBHILDON |
c96d7bec | 131 | "note_type", HILDON_NOTE_CONFIRMATION_BUTTON_TYPE, |
426d19f1 JS |
132 | #else // wxUSE_LIBHILDON |
133 | "note_type", HILDON_NOTE_TYPE_CONFIRMATION_BUTTON, | |
134 | #endif // wxUSE_LIBHILDON /wxUSE_LIBHILDON2 | |
c96d7bec VZ |
135 | "description", (const char *)GetFullMessage().utf8_str(), |
136 | "icon", stockIcon, | |
137 | NULL | |
138 | ); | |
426d19f1 | 139 | #else // !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2 |
b63b07a8 | 140 | GtkMessageType type = GTK_MESSAGE_ERROR; |
92763588 | 141 | GtkButtonsType buttons = GTK_BUTTONS_NONE; |
13a7abf9 | 142 | |
92763588 VZ |
143 | // when using custom labels, we have to add all the buttons ourselves |
144 | if ( !HasCustomLabels() ) | |
13a7abf9 | 145 | { |
7112cdd1 VZ |
146 | // "Help" button is not supported by predefined combinations so we |
147 | // always need to create the buttons manually when it's used. | |
148 | if ( !(m_dialogStyle & wxHELP) ) | |
92763588 | 149 | { |
7112cdd1 VZ |
150 | if ( m_dialogStyle & wxYES_NO ) |
151 | { | |
152 | if ( !(m_dialogStyle & wxCANCEL) ) | |
153 | buttons = GTK_BUTTONS_YES_NO; | |
154 | //else: no standard GTK_BUTTONS_YES_NO_CANCEL so leave as NONE | |
155 | } | |
156 | else if ( m_dialogStyle & wxOK ) | |
157 | { | |
158 | buttons = m_dialogStyle & wxCANCEL ? GTK_BUTTONS_OK_CANCEL | |
159 | : GTK_BUTTONS_OK; | |
160 | } | |
92763588 | 161 | } |
13a7abf9 | 162 | } |
b63b07a8 | 163 | |
a4578b0c | 164 | if ( !wxGTKImpl::ConvertMessageTypeFromWX(GetEffectiveIcon(), &type) ) |
e0ae1a0a | 165 | { |
7e3204b4 VZ |
166 | // if no style is explicitly specified, detect the suitable icon |
167 | // ourselves (this can be disabled by using wxICON_NONE) | |
2afb9e16 | 168 | type = m_dialogStyle & wxYES ? GTK_MESSAGE_QUESTION : GTK_MESSAGE_INFO; |
e0ae1a0a | 169 | } |
13a7abf9 | 170 | |
2afb9e16 | 171 | wxString message; |
2afb9e16 | 172 | bool needsExtMessage = false; |
4e621d24 | 173 | if (!m_extendedMessage.empty()) |
2afb9e16 VZ |
174 | { |
175 | message = m_message; | |
176 | needsExtMessage = true; | |
177 | } | |
4e621d24 | 178 | else // extended message not needed |
2afb9e16 VZ |
179 | { |
180 | message = GetFullMessage(); | |
181 | } | |
182 | ||
c96d7bec | 183 | m_widget = gtk_message_dialog_new(parent, |
b2ce5e1b | 184 | GTK_DIALOG_MODAL, |
2afb9e16 VZ |
185 | type, |
186 | buttons, | |
187 | "%s", | |
188 | (const char*)wxGTK_CONV(message)); | |
189 | ||
2afb9e16 VZ |
190 | if ( needsExtMessage ) |
191 | { | |
192 | gtk_message_dialog_format_secondary_text | |
193 | ( | |
194 | (GtkMessageDialog *)m_widget, | |
195 | "%s", | |
196 | (const char *)wxGTK_CONV(m_extendedMessage) | |
197 | ); | |
198 | } | |
426d19f1 | 199 | #endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2/!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2 |
2afb9e16 | 200 | |
9ff9d30c PC |
201 | g_object_ref(m_widget); |
202 | ||
13a7abf9 | 203 | if (m_caption != wxMessageBoxCaptionStr) |
b2ce5e1b | 204 | gtk_window_set_title(GTK_WINDOW(m_widget), wxGTK_CONV(m_caption)); |
13a7abf9 | 205 | |
c96d7bec | 206 | GtkDialog * const dlg = GTK_DIALOG(m_widget); |
92763588 | 207 | |
4566dcbe VZ |
208 | if ( m_dialogStyle & wxSTAY_ON_TOP ) |
209 | { | |
210 | gtk_window_set_keep_above(GTK_WINDOW(m_widget), TRUE); | |
211 | } | |
212 | ||
92763588 VZ |
213 | // we need to add buttons manually if we use custom labels or always for |
214 | // Yes/No/Cancel dialog as GTK+ doesn't support it natively and when using | |
215 | // Hildon we add all the buttons manually as it doesn't support too many of | |
216 | // the combinations we may have | |
426d19f1 | 217 | #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2 |
92763588 VZ |
218 | static const bool addButtons = true; |
219 | #else // !wxUSE_LIBHILDON | |
220 | const bool addButtons = buttons == GTK_BUTTONS_NONE; | |
221 | #endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON | |
222 | ||
7112cdd1 VZ |
223 | |
224 | if ( addButtons ) | |
92763588 | 225 | { |
7112cdd1 VZ |
226 | if ( m_dialogStyle & wxHELP ) |
227 | { | |
228 | gtk_dialog_add_button(dlg, wxGTK_CONV(GetHelpLabel()), | |
229 | GTK_RESPONSE_HELP); | |
230 | } | |
231 | ||
232 | if ( m_dialogStyle & wxYES_NO ) // Yes/No or Yes/No/Cancel dialog | |
c96d7bec | 233 | { |
5d3056f1 VZ |
234 | // Add the buttons in the correct order which is, according to |
235 | // http://library.gnome.org/devel/hig-book/stable/windows-alert.html.en | |
236 | // the following one: | |
237 | // | |
238 | // [Help] [Alternative] [Cancel] [Affirmative] | |
239 | ||
150c8eb9 VZ |
240 | gtk_dialog_add_button(dlg, wxGTK_CONV(GetNoLabel()), |
241 | GTK_RESPONSE_NO); | |
92763588 VZ |
242 | |
243 | if ( m_dialogStyle & wxCANCEL ) | |
244 | { | |
150c8eb9 | 245 | gtk_dialog_add_button(dlg, wxGTK_CONV(GetCancelLabel()), |
92763588 VZ |
246 | GTK_RESPONSE_CANCEL); |
247 | } | |
5d3056f1 VZ |
248 | |
249 | gtk_dialog_add_button(dlg, wxGTK_CONV(GetYesLabel()), | |
250 | GTK_RESPONSE_YES); | |
c96d7bec | 251 | } |
7112cdd1 | 252 | else // Ok or Ok/Cancel dialog |
150c8eb9 | 253 | { |
7112cdd1 VZ |
254 | gtk_dialog_add_button(dlg, wxGTK_CONV(GetOKLabel()), GTK_RESPONSE_OK); |
255 | if ( m_dialogStyle & wxCANCEL ) | |
256 | { | |
257 | gtk_dialog_add_button(dlg, wxGTK_CONV(GetCancelLabel()), | |
258 | GTK_RESPONSE_CANCEL); | |
259 | } | |
150c8eb9 | 260 | } |
13a7abf9 | 261 | } |
24689293 | 262 | |
7112cdd1 | 263 | gint defaultButton; |
24689293 | 264 | if ( m_dialogStyle & wxCANCEL_DEFAULT ) |
7112cdd1 VZ |
265 | defaultButton = GTK_RESPONSE_CANCEL; |
266 | else if ( m_dialogStyle & wxNO_DEFAULT ) | |
267 | defaultButton = GTK_RESPONSE_NO; | |
268 | else if ( m_dialogStyle & wxYES_NO ) | |
269 | defaultButton = GTK_RESPONSE_YES; | |
270 | else // No need to change the default value, whatever it is. | |
271 | defaultButton = GTK_RESPONSE_NONE; | |
272 | ||
273 | if ( defaultButton != GTK_RESPONSE_NONE ) | |
274 | gtk_dialog_set_default_response(dlg, defaultButton); | |
b2ce5e1b | 275 | } |
e5b50758 | 276 | |
b2ce5e1b RD |
277 | int wxMessageDialog::ShowModal() |
278 | { | |
643e9cf9 VS |
279 | WX_TESTING_SHOW_MODAL_HOOK(); |
280 | ||
7738af59 VZ |
281 | // break the mouse capture as it would interfere with modal dialog (see |
282 | // wxDialog::ShowModal) | |
283 | wxWindow * const win = wxWindow::GetCapture(); | |
284 | if ( win ) | |
285 | win->GTKReleaseMouseAndNotify(); | |
286 | ||
2afb9e16 VZ |
287 | if ( !m_widget ) |
288 | { | |
289 | GTKCreateMsgDialog(); | |
290 | wxCHECK_MSG( m_widget, wxID_CANCEL, | |
9a83f860 | 291 | wxT("failed to create GtkMessageDialog") ); |
2afb9e16 VZ |
292 | } |
293 | ||
3b439e60 RR |
294 | // This should be necessary, but otherwise the |
295 | // parent TLW will disappear.. | |
296 | if (m_parent) | |
297 | gtk_window_present( GTK_WINDOW(m_parent->m_widget) ); | |
e5b50758 | 298 | |
3c2f3a60 VZ |
299 | wxOpenModalDialogLocker modalLocker; |
300 | ||
b2ce5e1b | 301 | gint result = gtk_dialog_run(GTK_DIALOG(m_widget)); |
8ab75332 | 302 | GTKDisconnect(m_widget); |
b2ce5e1b | 303 | gtk_widget_destroy(m_widget); |
9ff9d30c | 304 | g_object_unref(m_widget); |
b2ce5e1b | 305 | m_widget = NULL; |
13a7abf9 VS |
306 | |
307 | switch (result) | |
308 | { | |
309 | default: | |
9a83f860 | 310 | wxFAIL_MSG(wxT("unexpected GtkMessageDialog return code")); |
13a7abf9 VS |
311 | // fall through |
312 | ||
b63b07a8 | 313 | case GTK_RESPONSE_CANCEL: |
0e4a7045 VS |
314 | case GTK_RESPONSE_DELETE_EVENT: |
315 | case GTK_RESPONSE_CLOSE: | |
13a7abf9 VS |
316 | return wxID_CANCEL; |
317 | case GTK_RESPONSE_OK: | |
318 | return wxID_OK; | |
319 | case GTK_RESPONSE_YES: | |
320 | return wxID_YES; | |
321 | case GTK_RESPONSE_NO: | |
322 | return wxID_NO; | |
7112cdd1 VZ |
323 | case GTK_RESPONSE_HELP: |
324 | return wxID_HELP; | |
13a7abf9 VS |
325 | } |
326 | } | |
327 | ||
b2ce5e1b | 328 | |
ff654490 | 329 | #endif // wxUSE_MSGDLG && !defined(__WXGPE__) |