1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/msgdlg.cpp
3 // Purpose: wxMessageDialog for GTK+2
4 // Author: Vaclav Slavik
8 // Copyright: (c) Vaclav Slavik, 2003
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
19 #if wxUSE_MSGDLG && !defined(__WXGPE__)
21 #include "wx/msgdlg.h"
27 #include "wx/testing.h"
30 #include "wx/gtk/private.h"
31 #include "wx/gtk/private/messagetype.h"
32 #include "wx/gtk/private/mnemonics.h"
33 #include "wx/gtk/private/dialogcount.h"
36 #include <hildon-widgets/hildon-note.h>
37 #endif // wxUSE_LIBHILDON
40 #include <hildon/hildon.h>
41 #endif // wxUSE_LIBHILDON2
43 IMPLEMENT_CLASS(wxMessageDialog
, wxDialog
)
45 wxMessageDialog::wxMessageDialog(wxWindow
*parent
,
46 const wxString
& message
,
47 const wxString
& caption
,
49 const wxPoint
& WXUNUSED(pos
))
52 GetParentForModalDialog(parent
, style
),
60 wxString
wxMessageDialog::GetDefaultYesLabel() const
65 wxString
wxMessageDialog::GetDefaultNoLabel() const
70 wxString
wxMessageDialog::GetDefaultOKLabel() const
75 wxString
wxMessageDialog::GetDefaultCancelLabel() const
77 return GTK_STOCK_CANCEL
;
80 wxString
wxMessageDialog::GetDefaultHelpLabel() const
82 return GTK_STOCK_HELP
;
85 void wxMessageDialog::DoSetCustomLabel(wxString
& var
, const ButtonLabel
& label
)
87 int stockId
= label
.GetStockId();
88 if ( stockId
== wxID_NONE
)
90 wxMessageDialogBase::DoSetCustomLabel(var
, label
);
91 var
= wxConvertMnemonicsToGTK(var
);
95 var
= wxGetStockGtkID(stockId
);
99 void wxMessageDialog::GTKCreateMsgDialog()
101 GtkWindow
* const parent
= m_parent
? GTK_WINDOW(m_parent
->m_widget
) : NULL
;
103 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
104 const char *stockIcon
= "";
106 switch ( GetEffectiveIcon() )
109 stockIcon
= "qgn_note_gene_syserror";
113 stockIcon
= "qgn_note_gene_syswarning";
116 case wxICON_QUESTION
:
117 stockIcon
= "qgn_note_confirm";
120 case wxICON_INFORMATION
:
121 stockIcon
= "qgn_note_info";
125 // there is no generic note creation function in public API so we have no
126 // choice but to use g_object_new() directly
127 m_widget
= (GtkWidget
*)g_object_new
131 "note_type", HILDON_NOTE_CONFIRMATION_BUTTON_TYPE
,
132 #else // wxUSE_LIBHILDON
133 "note_type", HILDON_NOTE_TYPE_CONFIRMATION_BUTTON
,
134 #endif // wxUSE_LIBHILDON /wxUSE_LIBHILDON2
135 "description", (const char *)GetFullMessage().utf8_str(),
139 #else // !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
140 GtkMessageType type
= GTK_MESSAGE_ERROR
;
141 GtkButtonsType buttons
= GTK_BUTTONS_NONE
;
143 // when using custom labels, we have to add all the buttons ourselves
144 if ( !HasCustomLabels() )
146 // "Help" button is not supported by predefined combinations so we
147 // always need to create the buttons manually when it's used.
148 if ( !(m_dialogStyle
& wxHELP
) )
150 if ( m_dialogStyle
& wxYES_NO
)
152 if ( !(m_dialogStyle
& wxCANCEL
) )
153 buttons
= GTK_BUTTONS_YES_NO
;
154 //else: no standard GTK_BUTTONS_YES_NO_CANCEL so leave as NONE
156 else if ( m_dialogStyle
& wxOK
)
158 buttons
= m_dialogStyle
& wxCANCEL
? GTK_BUTTONS_OK_CANCEL
164 if ( !wxGTKImpl::ConvertMessageTypeFromWX(GetEffectiveIcon(), &type
) )
166 // if no style is explicitly specified, detect the suitable icon
167 // ourselves (this can be disabled by using wxICON_NONE)
168 type
= m_dialogStyle
& wxYES
? GTK_MESSAGE_QUESTION
: GTK_MESSAGE_INFO
;
172 bool needsExtMessage
= false;
173 if (!m_extendedMessage
.empty())
176 needsExtMessage
= true;
178 else // extended message not needed
180 message
= GetFullMessage();
183 m_widget
= gtk_message_dialog_new(parent
,
188 (const char*)wxGTK_CONV(message
));
190 if ( needsExtMessage
)
192 gtk_message_dialog_format_secondary_text
194 (GtkMessageDialog
*)m_widget
,
196 (const char *)wxGTK_CONV(m_extendedMessage
)
199 #endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2/!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
201 g_object_ref(m_widget
);
203 if (m_caption
!= wxMessageBoxCaptionStr
)
204 gtk_window_set_title(GTK_WINDOW(m_widget
), wxGTK_CONV(m_caption
));
206 GtkDialog
* const dlg
= GTK_DIALOG(m_widget
);
208 if ( m_dialogStyle
& wxSTAY_ON_TOP
)
210 gtk_window_set_keep_above(GTK_WINDOW(m_widget
), TRUE
);
213 // we need to add buttons manually if we use custom labels or always for
214 // Yes/No/Cancel dialog as GTK+ doesn't support it natively and when using
215 // Hildon we add all the buttons manually as it doesn't support too many of
216 // the combinations we may have
217 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
218 static const bool addButtons
= true;
219 #else // !wxUSE_LIBHILDON
220 const bool addButtons
= buttons
== GTK_BUTTONS_NONE
;
221 #endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON
226 if ( m_dialogStyle
& wxHELP
)
228 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetHelpLabel()),
232 if ( m_dialogStyle
& wxYES_NO
) // Yes/No or Yes/No/Cancel dialog
234 // Add the buttons in the correct order which is, according to
235 // http://library.gnome.org/devel/hig-book/stable/windows-alert.html.en
236 // the following one:
238 // [Help] [Alternative] [Cancel] [Affirmative]
240 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetNoLabel()),
243 if ( m_dialogStyle
& wxCANCEL
)
245 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetCancelLabel()),
246 GTK_RESPONSE_CANCEL
);
249 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetYesLabel()),
252 else // Ok or Ok/Cancel dialog
254 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetOKLabel()), GTK_RESPONSE_OK
);
255 if ( m_dialogStyle
& wxCANCEL
)
257 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetCancelLabel()),
258 GTK_RESPONSE_CANCEL
);
264 if ( m_dialogStyle
& wxCANCEL_DEFAULT
)
265 defaultButton
= GTK_RESPONSE_CANCEL
;
266 else if ( m_dialogStyle
& wxNO_DEFAULT
)
267 defaultButton
= GTK_RESPONSE_NO
;
268 else if ( m_dialogStyle
& wxYES_NO
)
269 defaultButton
= GTK_RESPONSE_YES
;
270 else // No need to change the default value, whatever it is.
271 defaultButton
= GTK_RESPONSE_NONE
;
273 if ( defaultButton
!= GTK_RESPONSE_NONE
)
274 gtk_dialog_set_default_response(dlg
, defaultButton
);
277 int wxMessageDialog::ShowModal()
279 WX_TESTING_SHOW_MODAL_HOOK();
281 // break the mouse capture as it would interfere with modal dialog (see
282 // wxDialog::ShowModal)
283 wxWindow
* const win
= wxWindow::GetCapture();
285 win
->GTKReleaseMouseAndNotify();
289 GTKCreateMsgDialog();
290 wxCHECK_MSG( m_widget
, wxID_CANCEL
,
291 wxT("failed to create GtkMessageDialog") );
294 // This should be necessary, but otherwise the
295 // parent TLW will disappear..
297 gtk_window_present( GTK_WINDOW(m_parent
->m_widget
) );
299 wxOpenModalDialogLocker modalLocker
;
301 gint result
= gtk_dialog_run(GTK_DIALOG(m_widget
));
302 GTKDisconnect(m_widget
);
303 gtk_widget_destroy(m_widget
);
304 g_object_unref(m_widget
);
310 wxFAIL_MSG(wxT("unexpected GtkMessageDialog return code"));
313 case GTK_RESPONSE_CANCEL
:
314 case GTK_RESPONSE_DELETE_EVENT
:
315 case GTK_RESPONSE_CLOSE
:
317 case GTK_RESPONSE_OK
:
319 case GTK_RESPONSE_YES
:
321 case GTK_RESPONSE_NO
:
323 case GTK_RESPONSE_HELP
:
329 #endif // wxUSE_MSGDLG && !defined(__WXGPE__)