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"
28 #include "wx/gtk/private.h"
29 #include "wx/gtk/private/messagetype.h"
30 #include "wx/gtk/private/mnemonics.h"
33 #include <hildon-widgets/hildon-note.h>
34 #endif // wxUSE_LIBHILDON
37 #include <hildon/hildon.h>
38 #endif // wxUSE_LIBHILDON2
40 IMPLEMENT_CLASS(wxMessageDialog
, wxDialog
)
42 wxMessageDialog::wxMessageDialog(wxWindow
*parent
,
43 const wxString
& message
,
44 const wxString
& caption
,
46 const wxPoint
& WXUNUSED(pos
))
49 GetParentForModalDialog(parent
, style
),
57 wxString
wxMessageDialog::GetDefaultYesLabel() const
62 wxString
wxMessageDialog::GetDefaultNoLabel() const
67 wxString
wxMessageDialog::GetDefaultOKLabel() const
72 wxString
wxMessageDialog::GetDefaultCancelLabel() const
74 return GTK_STOCK_CANCEL
;
77 wxString
wxMessageDialog::GetDefaultHelpLabel() const
79 return GTK_STOCK_HELP
;
82 void wxMessageDialog::DoSetCustomLabel(wxString
& var
, const ButtonLabel
& label
)
84 int stockId
= label
.GetStockId();
85 if ( stockId
== wxID_NONE
)
87 wxMessageDialogBase::DoSetCustomLabel(var
, label
);
88 var
= wxConvertMnemonicsToGTK(var
);
92 var
= wxGetStockGtkID(stockId
);
96 void wxMessageDialog::GTKCreateMsgDialog()
98 GtkWindow
* const parent
= m_parent
? GTK_WINDOW(m_parent
->m_widget
) : NULL
;
100 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
101 const char *stockIcon
= "";
103 switch ( GetEffectiveIcon() )
106 stockIcon
= "qgn_note_gene_syserror";
110 stockIcon
= "qgn_note_gene_syswarning";
113 case wxICON_QUESTION
:
114 stockIcon
= "qgn_note_confirm";
117 case wxICON_INFORMATION
:
118 stockIcon
= "qgn_note_info";
122 // there is no generic note creation function in public API so we have no
123 // choice but to use g_object_new() directly
124 m_widget
= (GtkWidget
*)g_object_new
128 "note_type", HILDON_NOTE_CONFIRMATION_BUTTON_TYPE
,
129 #else // wxUSE_LIBHILDON
130 "note_type", HILDON_NOTE_TYPE_CONFIRMATION_BUTTON
,
131 #endif // wxUSE_LIBHILDON /wxUSE_LIBHILDON2
132 "description", (const char *)GetFullMessage().utf8_str(),
136 #else // !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
137 GtkMessageType type
= GTK_MESSAGE_ERROR
;
138 GtkButtonsType buttons
= GTK_BUTTONS_NONE
;
140 // when using custom labels, we have to add all the buttons ourselves
141 if ( !HasCustomLabels() )
143 // "Help" button is not supported by predefined combinations so we
144 // always need to create the buttons manually when it's used.
145 if ( !(m_dialogStyle
& wxHELP
) )
147 if ( m_dialogStyle
& wxYES_NO
)
149 if ( !(m_dialogStyle
& wxCANCEL
) )
150 buttons
= GTK_BUTTONS_YES_NO
;
151 //else: no standard GTK_BUTTONS_YES_NO_CANCEL so leave as NONE
153 else if ( m_dialogStyle
& wxOK
)
155 buttons
= m_dialogStyle
& wxCANCEL
? GTK_BUTTONS_OK_CANCEL
161 if ( !wxGTKImpl::ConvertMessageTypeFromWX(GetEffectiveIcon(), &type
) )
163 // if no style is explicitly specified, detect the suitable icon
164 // ourselves (this can be disabled by using wxICON_NONE)
165 type
= m_dialogStyle
& wxYES
? GTK_MESSAGE_QUESTION
: GTK_MESSAGE_INFO
;
169 #if GTK_CHECK_VERSION(2, 6, 0)
170 bool needsExtMessage
= false;
171 if (!m_extendedMessage
.empty()
173 && gtk_check_version(2, 6, 0) == NULL
178 needsExtMessage
= true;
180 else // extended message not needed or not supported
183 message
= GetFullMessage();
186 m_widget
= gtk_message_dialog_new(parent
,
191 (const char*)wxGTK_CONV(message
));
193 #if GTK_CHECK_VERSION(2, 6, 0)
194 if ( needsExtMessage
)
196 gtk_message_dialog_format_secondary_text
198 (GtkMessageDialog
*)m_widget
,
200 (const char *)wxGTK_CONV(m_extendedMessage
)
204 #endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2/!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
206 g_object_ref(m_widget
);
208 if (m_caption
!= wxMessageBoxCaptionStr
)
209 gtk_window_set_title(GTK_WINDOW(m_widget
), wxGTK_CONV(m_caption
));
211 GtkDialog
* const dlg
= GTK_DIALOG(m_widget
);
213 if ( m_dialogStyle
& wxSTAY_ON_TOP
)
215 gtk_window_set_keep_above(GTK_WINDOW(m_widget
), TRUE
);
218 // we need to add buttons manually if we use custom labels or always for
219 // Yes/No/Cancel dialog as GTK+ doesn't support it natively and when using
220 // Hildon we add all the buttons manually as it doesn't support too many of
221 // the combinations we may have
222 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
223 static const bool addButtons
= true;
224 #else // !wxUSE_LIBHILDON
225 const bool addButtons
= buttons
== GTK_BUTTONS_NONE
;
226 #endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON
231 if ( m_dialogStyle
& wxHELP
)
233 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetHelpLabel()),
237 if ( m_dialogStyle
& wxYES_NO
) // Yes/No or Yes/No/Cancel dialog
239 // Add the buttons in the correct order which is, according to
240 // http://library.gnome.org/devel/hig-book/stable/windows-alert.html.en
241 // the following one:
243 // [Help] [Alternative] [Cancel] [Affirmative]
245 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetNoLabel()),
248 if ( m_dialogStyle
& wxCANCEL
)
250 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetCancelLabel()),
251 GTK_RESPONSE_CANCEL
);
254 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetYesLabel()),
257 else // Ok or Ok/Cancel dialog
259 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetOKLabel()), GTK_RESPONSE_OK
);
260 if ( m_dialogStyle
& wxCANCEL
)
262 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetCancelLabel()),
263 GTK_RESPONSE_CANCEL
);
269 if ( m_dialogStyle
& wxCANCEL_DEFAULT
)
270 defaultButton
= GTK_RESPONSE_CANCEL
;
271 else if ( m_dialogStyle
& wxNO_DEFAULT
)
272 defaultButton
= GTK_RESPONSE_NO
;
273 else if ( m_dialogStyle
& wxYES_NO
)
274 defaultButton
= GTK_RESPONSE_YES
;
275 else // No need to change the default value, whatever it is.
276 defaultButton
= GTK_RESPONSE_NONE
;
278 if ( defaultButton
!= GTK_RESPONSE_NONE
)
279 gtk_dialog_set_default_response(dlg
, defaultButton
);
282 int wxMessageDialog::ShowModal()
284 // break the mouse capture as it would interfere with modal dialog (see
285 // wxDialog::ShowModal)
286 wxWindow
* const win
= wxWindow::GetCapture();
288 win
->GTKReleaseMouseAndNotify();
292 GTKCreateMsgDialog();
293 wxCHECK_MSG( m_widget
, wxID_CANCEL
,
294 wxT("failed to create GtkMessageDialog") );
297 // This should be necessary, but otherwise the
298 // parent TLW will disappear..
300 gtk_window_present( GTK_WINDOW(m_parent
->m_widget
) );
302 gint result
= gtk_dialog_run(GTK_DIALOG(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__)