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