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