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"
35 #include <hildon-widgets/hildon-note.h>
36 #endif // wxUSE_LIBHILDON
39 #include <hildon/hildon.h>
40 #endif // wxUSE_LIBHILDON2
42 IMPLEMENT_CLASS(wxMessageDialog
, wxDialog
)
44 wxMessageDialog::wxMessageDialog(wxWindow
*parent
,
45 const wxString
& message
,
46 const wxString
& caption
,
48 const wxPoint
& WXUNUSED(pos
))
51 GetParentForModalDialog(parent
, style
),
59 wxString
wxMessageDialog::GetDefaultYesLabel() const
64 wxString
wxMessageDialog::GetDefaultNoLabel() const
69 wxString
wxMessageDialog::GetDefaultOKLabel() const
74 wxString
wxMessageDialog::GetDefaultCancelLabel() const
76 return GTK_STOCK_CANCEL
;
79 wxString
wxMessageDialog::GetDefaultHelpLabel() const
81 return GTK_STOCK_HELP
;
84 void wxMessageDialog::DoSetCustomLabel(wxString
& var
, const ButtonLabel
& label
)
86 int stockId
= label
.GetStockId();
87 if ( stockId
== wxID_NONE
)
89 wxMessageDialogBase::DoSetCustomLabel(var
, label
);
90 var
= wxConvertMnemonicsToGTK(var
);
94 var
= wxGetStockGtkID(stockId
);
98 void wxMessageDialog::GTKCreateMsgDialog()
100 GtkWindow
* const parent
= m_parent
? GTK_WINDOW(m_parent
->m_widget
) : NULL
;
102 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
103 const char *stockIcon
= "";
105 switch ( GetEffectiveIcon() )
108 stockIcon
= "qgn_note_gene_syserror";
112 stockIcon
= "qgn_note_gene_syswarning";
115 case wxICON_QUESTION
:
116 stockIcon
= "qgn_note_confirm";
119 case wxICON_INFORMATION
:
120 stockIcon
= "qgn_note_info";
124 // there is no generic note creation function in public API so we have no
125 // choice but to use g_object_new() directly
126 m_widget
= (GtkWidget
*)g_object_new
130 "note_type", HILDON_NOTE_CONFIRMATION_BUTTON_TYPE
,
131 #else // wxUSE_LIBHILDON
132 "note_type", HILDON_NOTE_TYPE_CONFIRMATION_BUTTON
,
133 #endif // wxUSE_LIBHILDON /wxUSE_LIBHILDON2
134 "description", (const char *)GetFullMessage().utf8_str(),
138 #else // !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
139 GtkMessageType type
= GTK_MESSAGE_ERROR
;
140 GtkButtonsType buttons
= GTK_BUTTONS_NONE
;
142 // when using custom labels, we have to add all the buttons ourselves
143 if ( !HasCustomLabels() )
145 // "Help" button is not supported by predefined combinations so we
146 // always need to create the buttons manually when it's used.
147 if ( !(m_dialogStyle
& wxHELP
) )
149 if ( m_dialogStyle
& wxYES_NO
)
151 if ( !(m_dialogStyle
& wxCANCEL
) )
152 buttons
= GTK_BUTTONS_YES_NO
;
153 //else: no standard GTK_BUTTONS_YES_NO_CANCEL so leave as NONE
155 else if ( m_dialogStyle
& wxOK
)
157 buttons
= m_dialogStyle
& wxCANCEL
? GTK_BUTTONS_OK_CANCEL
163 if ( !wxGTKImpl::ConvertMessageTypeFromWX(GetEffectiveIcon(), &type
) )
165 // if no style is explicitly specified, detect the suitable icon
166 // ourselves (this can be disabled by using wxICON_NONE)
167 type
= m_dialogStyle
& wxYES
? GTK_MESSAGE_QUESTION
: GTK_MESSAGE_INFO
;
171 bool needsExtMessage
= false;
172 if (!m_extendedMessage
.empty())
175 needsExtMessage
= true;
177 else // extended message not needed
179 message
= GetFullMessage();
182 m_widget
= gtk_message_dialog_new(parent
,
187 (const char*)wxGTK_CONV(message
));
189 if ( needsExtMessage
)
191 gtk_message_dialog_format_secondary_text
193 (GtkMessageDialog
*)m_widget
,
195 (const char *)wxGTK_CONV(m_extendedMessage
)
198 #endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2/!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
200 g_object_ref(m_widget
);
202 if (m_caption
!= wxMessageBoxCaptionStr
)
203 gtk_window_set_title(GTK_WINDOW(m_widget
), wxGTK_CONV(m_caption
));
205 GtkDialog
* const dlg
= GTK_DIALOG(m_widget
);
207 if ( m_dialogStyle
& wxSTAY_ON_TOP
)
209 gtk_window_set_keep_above(GTK_WINDOW(m_widget
), TRUE
);
212 // we need to add buttons manually if we use custom labels or always for
213 // Yes/No/Cancel dialog as GTK+ doesn't support it natively and when using
214 // Hildon we add all the buttons manually as it doesn't support too many of
215 // the combinations we may have
216 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
217 static const bool addButtons
= true;
218 #else // !wxUSE_LIBHILDON
219 const bool addButtons
= buttons
== GTK_BUTTONS_NONE
;
220 #endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON
225 if ( m_dialogStyle
& wxHELP
)
227 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetHelpLabel()),
231 if ( m_dialogStyle
& wxYES_NO
) // Yes/No or Yes/No/Cancel dialog
233 // Add the buttons in the correct order which is, according to
234 // http://library.gnome.org/devel/hig-book/stable/windows-alert.html.en
235 // the following one:
237 // [Help] [Alternative] [Cancel] [Affirmative]
239 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetNoLabel()),
242 if ( m_dialogStyle
& wxCANCEL
)
244 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetCancelLabel()),
245 GTK_RESPONSE_CANCEL
);
248 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetYesLabel()),
251 else // Ok or Ok/Cancel dialog
253 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetOKLabel()), GTK_RESPONSE_OK
);
254 if ( m_dialogStyle
& wxCANCEL
)
256 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetCancelLabel()),
257 GTK_RESPONSE_CANCEL
);
263 if ( m_dialogStyle
& wxCANCEL_DEFAULT
)
264 defaultButton
= GTK_RESPONSE_CANCEL
;
265 else if ( m_dialogStyle
& wxNO_DEFAULT
)
266 defaultButton
= GTK_RESPONSE_NO
;
267 else if ( m_dialogStyle
& wxYES_NO
)
268 defaultButton
= GTK_RESPONSE_YES
;
269 else // No need to change the default value, whatever it is.
270 defaultButton
= GTK_RESPONSE_NONE
;
272 if ( defaultButton
!= GTK_RESPONSE_NONE
)
273 gtk_dialog_set_default_response(dlg
, defaultButton
);
276 int wxMessageDialog::ShowModal()
278 WX_TESTING_SHOW_MODAL_HOOK();
280 // break the mouse capture as it would interfere with modal dialog (see
281 // wxDialog::ShowModal)
282 wxWindow
* const win
= wxWindow::GetCapture();
284 win
->GTKReleaseMouseAndNotify();
288 GTKCreateMsgDialog();
289 wxCHECK_MSG( m_widget
, wxID_CANCEL
,
290 wxT("failed to create GtkMessageDialog") );
293 // This should be necessary, but otherwise the
294 // parent TLW will disappear..
296 gtk_window_present( GTK_WINDOW(m_parent
->m_widget
) );
298 gint result
= gtk_dialog_run(GTK_DIALOG(m_widget
));
299 GTKDisconnect(m_widget
);
300 gtk_widget_destroy(m_widget
);
301 g_object_unref(m_widget
);
307 wxFAIL_MSG(wxT("unexpected GtkMessageDialog return code"));
310 case GTK_RESPONSE_CANCEL
:
311 case GTK_RESPONSE_DELETE_EVENT
:
312 case GTK_RESPONSE_CLOSE
:
314 case GTK_RESPONSE_OK
:
316 case GTK_RESPONSE_YES
:
318 case GTK_RESPONSE_NO
:
320 case GTK_RESPONSE_HELP
:
326 #endif // wxUSE_MSGDLG && !defined(__WXGPE__)