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