Fix button order in wxGTK wxMessageDialog and wxStdDialogButtonSizer.
[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 // Add the buttons in the correct order which is, according to
219 // http://library.gnome.org/devel/hig-book/stable/windows-alert.html.en
220 // the following one:
221 //
222 // [Help] [Alternative] [Cancel] [Affirmative]
223
224 gtk_dialog_add_button(dlg, wxGTK_CONV(GetNoLabel()),
225 GTK_RESPONSE_NO);
226
227 if ( m_dialogStyle & wxCANCEL )
228 {
229 gtk_dialog_add_button(dlg, wxGTK_CONV(GetCancelLabel()),
230 GTK_RESPONSE_CANCEL);
231 }
232
233 gtk_dialog_add_button(dlg, wxGTK_CONV(GetYesLabel()),
234 GTK_RESPONSE_YES);
235 }
236
237 // it'd probably be harmless to call gtk_dialog_set_default_response()
238 // twice but why do it if we're going to change the default below
239 // anyhow
240 if ( !(m_dialogStyle & wxCANCEL_DEFAULT) )
241 {
242 gtk_dialog_set_default_response(dlg,
243 m_dialogStyle & wxNO_DEFAULT
244 ? GTK_RESPONSE_NO
245 : GTK_RESPONSE_YES);
246 }
247 }
248 else if ( addButtons ) // Ok or Ok/Cancel dialog
249 {
250 gtk_dialog_add_button(dlg, wxGTK_CONV(GetOKLabel()), GTK_RESPONSE_OK);
251 if ( m_dialogStyle & wxCANCEL )
252 {
253 gtk_dialog_add_button(dlg, wxGTK_CONV(GetCancelLabel()),
254 GTK_RESPONSE_CANCEL);
255 }
256 }
257
258 if ( m_dialogStyle & wxCANCEL_DEFAULT )
259 {
260 gtk_dialog_set_default_response(dlg, GTK_RESPONSE_CANCEL);
261 }
262 }
263
264 int wxMessageDialog::ShowModal()
265 {
266 // break the mouse capture as it would interfere with modal dialog (see
267 // wxDialog::ShowModal)
268 wxWindow * const win = wxWindow::GetCapture();
269 if ( win )
270 win->GTKReleaseMouseAndNotify();
271
272 if ( !m_widget )
273 {
274 GTKCreateMsgDialog();
275 wxCHECK_MSG( m_widget, wxID_CANCEL,
276 wxT("failed to create GtkMessageDialog") );
277 }
278
279 // This should be necessary, but otherwise the
280 // parent TLW will disappear..
281 if (m_parent)
282 gtk_window_present( GTK_WINDOW(m_parent->m_widget) );
283
284 gint result = gtk_dialog_run(GTK_DIALOG(m_widget));
285 gtk_widget_destroy(m_widget);
286 g_object_unref(m_widget);
287 m_widget = NULL;
288
289 switch (result)
290 {
291 default:
292 wxFAIL_MSG(wxT("unexpected GtkMessageDialog return code"));
293 // fall through
294
295 case GTK_RESPONSE_CANCEL:
296 case GTK_RESPONSE_DELETE_EVENT:
297 case GTK_RESPONSE_CLOSE:
298 return wxID_CANCEL;
299 case GTK_RESPONSE_OK:
300 return wxID_OK;
301 case GTK_RESPONSE_YES:
302 return wxID_YES;
303 case GTK_RESPONSE_NO:
304 return wxID_NO;
305 }
306 }
307
308
309 #endif // wxUSE_MSGDLG && !defined(__WXGPE__)