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