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/messagetype.h"
29 #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
))
47 : wxMessageDialogWithCustomLabels(GetParentForModalDialog(parent
),
54 wxString
wxMessageDialog::GetDefaultYesLabel() const
59 wxString
wxMessageDialog::GetDefaultNoLabel() const
64 wxString
wxMessageDialog::GetDefaultOKLabel() const
69 wxString
wxMessageDialog::GetDefaultCancelLabel() const
71 return GTK_STOCK_CANCEL
;
74 void wxMessageDialog::DoSetCustomLabel(wxString
& var
, const ButtonLabel
& label
)
76 int stockId
= label
.GetStockId();
77 if ( stockId
== wxID_NONE
)
79 wxMessageDialogWithCustomLabels::DoSetCustomLabel(var
, label
);
80 var
= wxConvertMnemonicsToGTK(var
);
84 var
= wxGetStockGtkID(stockId
);
88 void wxMessageDialog::GTKCreateMsgDialog()
90 GtkWindow
* const parent
= m_parent
? GTK_WINDOW(m_parent
->m_widget
) : NULL
;
92 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
93 const char *stockIcon
= "";
95 switch ( GetEffectiveIcon() )
98 stockIcon
= "qgn_note_gene_syserror";
102 stockIcon
= "qgn_note_gene_syswarning";
105 case wxICON_QUESTION
:
106 stockIcon
= "qgn_note_confirm";
109 case wxICON_INFORMATION
:
110 stockIcon
= "qgn_note_info";
114 // there is no generic note creation function in public API so we have no
115 // choice but to use g_object_new() directly
116 m_widget
= (GtkWidget
*)g_object_new
120 "note_type", HILDON_NOTE_CONFIRMATION_BUTTON_TYPE
,
121 #else // wxUSE_LIBHILDON
122 "note_type", HILDON_NOTE_TYPE_CONFIRMATION_BUTTON
,
123 #endif // wxUSE_LIBHILDON /wxUSE_LIBHILDON2
124 "description", (const char *)GetFullMessage().utf8_str(),
128 #else // !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
129 GtkMessageType type
= GTK_MESSAGE_ERROR
;
130 GtkButtonsType buttons
= GTK_BUTTONS_NONE
;
132 // when using custom labels, we have to add all the buttons ourselves
133 if ( !HasCustomLabels() )
135 if ( m_dialogStyle
& wxYES_NO
)
137 if ( !(m_dialogStyle
& wxCANCEL
) )
138 buttons
= GTK_BUTTONS_YES_NO
;
139 //else: no standard GTK_BUTTONS_YES_NO_CANCEL so leave as NONE
141 else if ( m_dialogStyle
& wxOK
)
143 buttons
= m_dialogStyle
& wxCANCEL
? GTK_BUTTONS_OK_CANCEL
148 if ( !wxGTKImpl::ConvertMessageTypeFromWX(GetEffectiveIcon(), &type
) )
150 // if no style is explicitly specified, detect the suitable icon
151 // ourselves (this can be disabled by using wxICON_NONE)
152 type
= m_dialogStyle
& wxYES
? GTK_MESSAGE_QUESTION
: GTK_MESSAGE_INFO
;
156 #if GTK_CHECK_VERSION(2, 6, 0)
157 bool needsExtMessage
= false;
158 if ( gtk_check_version(2, 6, 0) == NULL
&& !m_extendedMessage
.empty() )
161 needsExtMessage
= true;
163 else // extended message not needed or not supported
166 message
= GetFullMessage();
169 m_widget
= gtk_message_dialog_new(parent
,
174 (const char*)wxGTK_CONV(message
));
176 #if GTK_CHECK_VERSION(2, 6, 0)
177 if ( needsExtMessage
)
179 gtk_message_dialog_format_secondary_text
181 (GtkMessageDialog
*)m_widget
,
183 (const char *)wxGTK_CONV(m_extendedMessage
)
187 #endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2/!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
189 g_object_ref(m_widget
);
191 if (m_caption
!= wxMessageBoxCaptionStr
)
192 gtk_window_set_title(GTK_WINDOW(m_widget
), wxGTK_CONV(m_caption
));
194 GtkDialog
* const dlg
= GTK_DIALOG(m_widget
);
196 if ( m_dialogStyle
& wxSTAY_ON_TOP
)
198 gtk_window_set_keep_above(GTK_WINDOW(m_widget
), TRUE
);
201 // we need to add buttons manually if we use custom labels or always for
202 // Yes/No/Cancel dialog as GTK+ doesn't support it natively and when using
203 // Hildon we add all the buttons manually as it doesn't support too many of
204 // the combinations we may have
205 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
206 static const bool addButtons
= true;
207 #else // !wxUSE_LIBHILDON
208 const bool addButtons
= buttons
== GTK_BUTTONS_NONE
;
209 #endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON
211 if ( m_dialogStyle
& wxYES_NO
) // Yes/No or Yes/No/Cancel dialog
215 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetNoLabel()),
217 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetYesLabel()),
220 if ( m_dialogStyle
& wxCANCEL
)
222 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetCancelLabel()),
223 GTK_RESPONSE_CANCEL
);
227 // it'd probably be harmless to call gtk_dialog_set_default_response()
228 // twice but why do it if we're going to change the default below
230 if ( !(m_dialogStyle
& wxCANCEL_DEFAULT
) )
232 gtk_dialog_set_default_response(dlg
,
233 m_dialogStyle
& wxNO_DEFAULT
238 else if ( addButtons
) // Ok or Ok/Cancel dialog
240 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetOKLabel()), GTK_RESPONSE_OK
);
241 if ( m_dialogStyle
& wxCANCEL
)
243 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetCancelLabel()),
244 GTK_RESPONSE_CANCEL
);
248 if ( m_dialogStyle
& wxCANCEL_DEFAULT
)
250 gtk_dialog_set_default_response(dlg
, GTK_RESPONSE_CANCEL
);
254 int wxMessageDialog::ShowModal()
256 // break the mouse capture as it would interfere with modal dialog (see
257 // wxDialog::ShowModal)
258 wxWindow
* const win
= wxWindow::GetCapture();
260 win
->GTKReleaseMouseAndNotify();
264 GTKCreateMsgDialog();
265 wxCHECK_MSG( m_widget
, wxID_CANCEL
,
266 wxT("failed to create GtkMessageDialog") );
269 // This should be necessary, but otherwise the
270 // parent TLW will disappear..
272 gtk_window_present( GTK_WINDOW(m_parent
->m_widget
) );
274 gint result
= gtk_dialog_run(GTK_DIALOG(m_widget
));
275 gtk_widget_destroy(m_widget
);
276 g_object_unref(m_widget
);
282 wxFAIL_MSG(wxT("unexpected GtkMessageDialog return code"));
285 case GTK_RESPONSE_CANCEL
:
286 case GTK_RESPONSE_DELETE_EVENT
:
287 case GTK_RESPONSE_CLOSE
:
289 case GTK_RESPONSE_OK
:
291 case GTK_RESPONSE_YES
:
293 case GTK_RESPONSE_NO
:
299 #endif // wxUSE_MSGDLG && !defined(__WXGPE__)