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/gtk/private.h"
28 #include "wx/gtk/private/mnemonics.h"
32 #include <hildon-widgets/hildon-note.h>
33 #endif // wxUSE_LIBHILDON
35 IMPLEMENT_CLASS(wxMessageDialog
, wxDialog
)
37 wxMessageDialog::wxMessageDialog(wxWindow
*parent
,
38 const wxString
& message
,
39 const wxString
& caption
,
41 const wxPoint
& WXUNUSED(pos
))
42 : wxMessageDialogWithCustomLabels(GetParentForModalDialog(parent
),
49 wxString
wxMessageDialog::GetDefaultYesLabel() const
54 wxString
wxMessageDialog::GetDefaultNoLabel() const
59 wxString
wxMessageDialog::GetDefaultOKLabel() const
64 wxString
wxMessageDialog::GetDefaultCancelLabel() const
66 return GTK_STOCK_CANCEL
;
69 void wxMessageDialog::DoSetCustomLabel(wxString
& var
, const ButtonLabel
& label
)
71 int stockId
= label
.GetStockId();
72 if ( stockId
== wxID_NONE
)
74 wxMessageDialogWithCustomLabels::DoSetCustomLabel(var
, label
);
75 var
= wxConvertMnemonicsToGTK(var
);
79 var
= wxGetStockGtkID(stockId
);
83 void wxMessageDialog::GTKCreateMsgDialog()
85 GtkWindow
* const parent
= m_parent
? GTK_WINDOW(m_parent
->m_widget
) : NULL
;
88 const char *stockIcon
;
89 if ( m_dialogStyle
& wxICON_ERROR
)
90 stockIcon
= "qgn_note_gene_syserror";
91 else if ( m_dialogStyle
& wxICON_EXCLAMATION
)
92 stockIcon
= "qgn_note_gene_syswarning";
93 else if ( m_dialogStyle
& wxICON_INFORMATION
)
94 stockIcon
= "qgn_note_info";
95 else if ( m_dialogStyle
& wxICON_QUESTION
)
96 stockIcon
= "qgn_note_confirm";
100 // there is no generic note creation function in public API so we have no
101 // choice but to use g_object_new() directly
102 m_widget
= (GtkWidget
*)g_object_new
105 "note_type", HILDON_NOTE_CONFIRMATION_BUTTON_TYPE
,
106 "description", (const char *)GetFullMessage().utf8_str(),
110 #else // !wxUSE_LIBHILDON
111 GtkMessageType type
= GTK_MESSAGE_ERROR
;
112 GtkButtonsType buttons
= GTK_BUTTONS_NONE
;
114 // when using custom labels, we have to add all the buttons ourselves
115 if ( !HasCustomLabels() )
117 if ( m_dialogStyle
& wxYES_NO
)
119 if ( !(m_dialogStyle
& wxCANCEL
) )
120 buttons
= GTK_BUTTONS_YES_NO
;
121 //else: no standard GTK_BUTTONS_YES_NO_CANCEL so leave as NONE
123 else if ( m_dialogStyle
& wxOK
)
125 buttons
= m_dialogStyle
& wxCANCEL
? GTK_BUTTONS_OK_CANCEL
130 if (m_dialogStyle
& wxICON_EXCLAMATION
)
131 type
= GTK_MESSAGE_WARNING
;
132 else if (m_dialogStyle
& wxICON_ERROR
)
133 type
= GTK_MESSAGE_ERROR
;
134 else if (m_dialogStyle
& wxICON_INFORMATION
)
135 type
= GTK_MESSAGE_INFO
;
136 else if (m_dialogStyle
& wxICON_QUESTION
)
137 type
= GTK_MESSAGE_QUESTION
;
140 // GTK+ doesn't have a "typeless" msg box, so try to auto detect...
141 type
= m_dialogStyle
& wxYES
? GTK_MESSAGE_QUESTION
: GTK_MESSAGE_INFO
;
145 #if GTK_CHECK_VERSION(2, 6, 0)
146 bool needsExtMessage
= false;
147 if ( gtk_check_version(2, 6, 0) == NULL
&& !m_extendedMessage
.empty() )
150 needsExtMessage
= true;
152 else // extended message not needed or not supported
155 message
= GetFullMessage();
158 m_widget
= gtk_message_dialog_new(parent
,
163 (const char*)wxGTK_CONV(message
));
165 #if GTK_CHECK_VERSION(2, 6, 0)
166 if ( needsExtMessage
)
168 gtk_message_dialog_format_secondary_text
170 (GtkMessageDialog
*)m_widget
,
172 (const char *)wxGTK_CONV(m_extendedMessage
)
176 #endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON
178 g_object_ref(m_widget
);
180 if (m_caption
!= wxMessageBoxCaptionStr
)
181 gtk_window_set_title(GTK_WINDOW(m_widget
), wxGTK_CONV(m_caption
));
183 GtkDialog
* const dlg
= GTK_DIALOG(m_widget
);
185 // we need to add buttons manually if we use custom labels or always for
186 // Yes/No/Cancel dialog as GTK+ doesn't support it natively and when using
187 // Hildon we add all the buttons manually as it doesn't support too many of
188 // the combinations we may have
190 static const bool addButtons
= true;
191 #else // !wxUSE_LIBHILDON
192 const bool addButtons
= buttons
== GTK_BUTTONS_NONE
;
193 #endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON
195 if ( m_dialogStyle
& wxYES_NO
) // Yes/No or Yes/No/Cancel dialog
199 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetNoLabel()),
201 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetYesLabel()),
204 if ( m_dialogStyle
& wxCANCEL
)
206 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetCancelLabel()),
207 GTK_RESPONSE_CANCEL
);
211 // it'd probably be harmless to call gtk_dialog_set_default_response()
212 // twice but why do it if we're going to change the default below
214 if ( !(m_dialogStyle
& wxCANCEL_DEFAULT
) )
216 gtk_dialog_set_default_response(dlg
,
217 m_dialogStyle
& wxNO_DEFAULT
222 else if ( addButtons
) // Ok or Ok/Cancel dialog
224 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetOKLabel()), GTK_RESPONSE_OK
);
225 if ( m_dialogStyle
& wxCANCEL
)
227 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetCancelLabel()),
228 GTK_RESPONSE_CANCEL
);
232 if ( m_dialogStyle
& wxCANCEL_DEFAULT
)
234 gtk_dialog_set_default_response(dlg
, GTK_RESPONSE_CANCEL
);
238 int wxMessageDialog::ShowModal()
240 // break the mouse capture as it would interfere with modal dialog (see
241 // wxDialog::ShowModal)
242 wxWindow
* const win
= wxWindow::GetCapture();
244 win
->GTKReleaseMouseAndNotify();
248 GTKCreateMsgDialog();
249 wxCHECK_MSG( m_widget
, wxID_CANCEL
,
250 _T("failed to create GtkMessageDialog") );
253 // This should be necessary, but otherwise the
254 // parent TLW will disappear..
256 gtk_window_present( GTK_WINDOW(m_parent
->m_widget
) );
258 gint result
= gtk_dialog_run(GTK_DIALOG(m_widget
));
259 gtk_widget_destroy(m_widget
);
260 g_object_unref(m_widget
);
266 wxFAIL_MSG(_T("unexpected GtkMessageDialog return code"));
269 case GTK_RESPONSE_CANCEL
:
270 case GTK_RESPONSE_DELETE_EVENT
:
271 case GTK_RESPONSE_CLOSE
:
273 case GTK_RESPONSE_OK
:
275 case GTK_RESPONSE_YES
:
277 case GTK_RESPONSE_NO
:
283 #endif // wxUSE_MSGDLG && !defined(__WXGPE__)