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
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 void wxMessageDialog::DoSetCustomLabel(wxString
& var
, const ButtonLabel
& label
)
79 int stockId
= label
.GetStockId();
80 if ( stockId
== wxID_NONE
)
82 wxMessageDialogWithCustomLabels::DoSetCustomLabel(var
, label
);
83 var
= wxConvertMnemonicsToGTK(var
);
87 var
= wxGetStockGtkID(stockId
);
91 void wxMessageDialog::GTKCreateMsgDialog()
93 GtkWindow
* const parent
= m_parent
? GTK_WINDOW(m_parent
->m_widget
) : NULL
;
95 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
96 const char *stockIcon
= "";
98 switch ( GetEffectiveIcon() )
101 stockIcon
= "qgn_note_gene_syserror";
105 stockIcon
= "qgn_note_gene_syswarning";
108 case wxICON_QUESTION
:
109 stockIcon
= "qgn_note_confirm";
112 case wxICON_INFORMATION
:
113 stockIcon
= "qgn_note_info";
117 // there is no generic note creation function in public API so we have no
118 // choice but to use g_object_new() directly
119 m_widget
= (GtkWidget
*)g_object_new
123 "note_type", HILDON_NOTE_CONFIRMATION_BUTTON_TYPE
,
124 #else // wxUSE_LIBHILDON
125 "note_type", HILDON_NOTE_TYPE_CONFIRMATION_BUTTON
,
126 #endif // wxUSE_LIBHILDON /wxUSE_LIBHILDON2
127 "description", (const char *)GetFullMessage().utf8_str(),
131 #else // !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
132 GtkMessageType type
= GTK_MESSAGE_ERROR
;
133 GtkButtonsType buttons
= GTK_BUTTONS_NONE
;
135 // when using custom labels, we have to add all the buttons ourselves
136 if ( !HasCustomLabels() )
138 if ( m_dialogStyle
& wxYES_NO
)
140 if ( !(m_dialogStyle
& wxCANCEL
) )
141 buttons
= GTK_BUTTONS_YES_NO
;
142 //else: no standard GTK_BUTTONS_YES_NO_CANCEL so leave as NONE
144 else if ( m_dialogStyle
& wxOK
)
146 buttons
= m_dialogStyle
& wxCANCEL
? GTK_BUTTONS_OK_CANCEL
151 if ( !wxGTKImpl::ConvertMessageTypeFromWX(GetEffectiveIcon(), &type
) )
153 // if no style is explicitly specified, detect the suitable icon
154 // ourselves (this can be disabled by using wxICON_NONE)
155 type
= m_dialogStyle
& wxYES
? GTK_MESSAGE_QUESTION
: GTK_MESSAGE_INFO
;
159 #if GTK_CHECK_VERSION(2, 6, 0)
160 bool needsExtMessage
= false;
161 if ( gtk_check_version(2, 6, 0) == NULL
&& !m_extendedMessage
.empty() )
164 needsExtMessage
= true;
166 else // extended message not needed or not supported
169 message
= GetFullMessage();
172 m_widget
= gtk_message_dialog_new(parent
,
177 (const char*)wxGTK_CONV(message
));
179 #if GTK_CHECK_VERSION(2, 6, 0)
180 if ( needsExtMessage
)
182 gtk_message_dialog_format_secondary_text
184 (GtkMessageDialog
*)m_widget
,
186 (const char *)wxGTK_CONV(m_extendedMessage
)
190 #endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2/!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
192 g_object_ref(m_widget
);
194 if (m_caption
!= wxMessageBoxCaptionStr
)
195 gtk_window_set_title(GTK_WINDOW(m_widget
), wxGTK_CONV(m_caption
));
197 GtkDialog
* const dlg
= GTK_DIALOG(m_widget
);
199 if ( m_dialogStyle
& wxSTAY_ON_TOP
)
201 gtk_window_set_keep_above(GTK_WINDOW(m_widget
), TRUE
);
204 // we need to add buttons manually if we use custom labels or always for
205 // Yes/No/Cancel dialog as GTK+ doesn't support it natively and when using
206 // Hildon we add all the buttons manually as it doesn't support too many of
207 // the combinations we may have
208 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
209 static const bool addButtons
= true;
210 #else // !wxUSE_LIBHILDON
211 const bool addButtons
= buttons
== GTK_BUTTONS_NONE
;
212 #endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON
214 if ( m_dialogStyle
& wxYES_NO
) // Yes/No or Yes/No/Cancel dialog
218 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetNoLabel()),
220 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetYesLabel()),
223 if ( m_dialogStyle
& wxCANCEL
)
225 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetCancelLabel()),
226 GTK_RESPONSE_CANCEL
);
230 // it'd probably be harmless to call gtk_dialog_set_default_response()
231 // twice but why do it if we're going to change the default below
233 if ( !(m_dialogStyle
& wxCANCEL_DEFAULT
) )
235 gtk_dialog_set_default_response(dlg
,
236 m_dialogStyle
& wxNO_DEFAULT
241 else if ( addButtons
) // Ok or Ok/Cancel dialog
243 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetOKLabel()), GTK_RESPONSE_OK
);
244 if ( m_dialogStyle
& wxCANCEL
)
246 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetCancelLabel()),
247 GTK_RESPONSE_CANCEL
);
251 if ( m_dialogStyle
& wxCANCEL_DEFAULT
)
253 gtk_dialog_set_default_response(dlg
, GTK_RESPONSE_CANCEL
);
257 int wxMessageDialog::ShowModal()
259 // break the mouse capture as it would interfere with modal dialog (see
260 // wxDialog::ShowModal)
261 wxWindow
* const win
= wxWindow::GetCapture();
263 win
->GTKReleaseMouseAndNotify();
267 GTKCreateMsgDialog();
268 wxCHECK_MSG( m_widget
, wxID_CANCEL
,
269 wxT("failed to create GtkMessageDialog") );
272 // This should be necessary, but otherwise the
273 // parent TLW will disappear..
275 gtk_window_present( GTK_WINDOW(m_parent
->m_widget
) );
277 gint result
= gtk_dialog_run(GTK_DIALOG(m_widget
));
278 gtk_widget_destroy(m_widget
);
279 g_object_unref(m_widget
);
285 wxFAIL_MSG(wxT("unexpected GtkMessageDialog return code"));
288 case GTK_RESPONSE_CANCEL
:
289 case GTK_RESPONSE_DELETE_EVENT
:
290 case GTK_RESPONSE_CLOSE
:
292 case GTK_RESPONSE_OK
:
294 case GTK_RESPONSE_YES
:
296 case GTK_RESPONSE_NO
:
302 #endif // wxUSE_MSGDLG && !defined(__WXGPE__)