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
36 #include <hildon/hildon.h>
37 #endif // wxUSE_LIBHILDON2
39 IMPLEMENT_CLASS(wxMessageDialog
, wxDialog
)
41 wxMessageDialog::wxMessageDialog(wxWindow
*parent
,
42 const wxString
& message
,
43 const wxString
& caption
,
45 const wxPoint
& WXUNUSED(pos
))
46 : wxMessageDialogWithCustomLabels(GetParentForModalDialog(parent
),
53 wxString
wxMessageDialog::GetDefaultYesLabel() const
58 wxString
wxMessageDialog::GetDefaultNoLabel() const
63 wxString
wxMessageDialog::GetDefaultOKLabel() const
68 wxString
wxMessageDialog::GetDefaultCancelLabel() const
70 return GTK_STOCK_CANCEL
;
73 void wxMessageDialog::DoSetCustomLabel(wxString
& var
, const ButtonLabel
& label
)
75 int stockId
= label
.GetStockId();
76 if ( stockId
== wxID_NONE
)
78 wxMessageDialogWithCustomLabels::DoSetCustomLabel(var
, label
);
79 var
= wxConvertMnemonicsToGTK(var
);
83 var
= wxGetStockGtkID(stockId
);
87 void wxMessageDialog::GTKCreateMsgDialog()
89 GtkWindow
* const parent
= m_parent
? GTK_WINDOW(m_parent
->m_widget
) : NULL
;
91 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
92 const char *stockIcon
;
93 if ( m_dialogStyle
& wxICON_NONE
)
95 else if ( m_dialogStyle
& wxICON_ERROR
)
96 stockIcon
= "qgn_note_gene_syserror";
97 else if ( m_dialogStyle
& wxICON_EXCLAMATION
)
98 stockIcon
= "qgn_note_gene_syswarning";
99 else if ( m_dialogStyle
& wxICON_INFORMATION
)
100 stockIcon
= "qgn_note_info";
101 else if ( m_dialogStyle
& wxICON_QUESTION
)
102 stockIcon
= "qgn_note_confirm";
106 // there is no generic note creation function in public API so we have no
107 // choice but to use g_object_new() directly
108 m_widget
= (GtkWidget
*)g_object_new
112 "note_type", HILDON_NOTE_CONFIRMATION_BUTTON_TYPE
,
113 #else // wxUSE_LIBHILDON
114 "note_type", HILDON_NOTE_TYPE_CONFIRMATION_BUTTON
,
115 #endif // wxUSE_LIBHILDON /wxUSE_LIBHILDON2
116 "description", (const char *)GetFullMessage().utf8_str(),
120 #else // !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
121 GtkMessageType type
= GTK_MESSAGE_ERROR
;
122 GtkButtonsType buttons
= GTK_BUTTONS_NONE
;
124 // when using custom labels, we have to add all the buttons ourselves
125 if ( !HasCustomLabels() )
127 if ( m_dialogStyle
& wxYES_NO
)
129 if ( !(m_dialogStyle
& wxCANCEL
) )
130 buttons
= GTK_BUTTONS_YES_NO
;
131 //else: no standard GTK_BUTTONS_YES_NO_CANCEL so leave as NONE
133 else if ( m_dialogStyle
& wxOK
)
135 buttons
= m_dialogStyle
& wxCANCEL
? GTK_BUTTONS_OK_CANCEL
141 if ( gtk_check_version(2, 10, 0) == NULL
&& (m_dialogStyle
& wxICON_NONE
))
142 type
= GTK_MESSAGE_OTHER
;
144 #endif // __WXGTK210__
145 if (m_dialogStyle
& wxICON_EXCLAMATION
)
146 type
= GTK_MESSAGE_WARNING
;
147 else if (m_dialogStyle
& wxICON_ERROR
)
148 type
= GTK_MESSAGE_ERROR
;
149 else if (m_dialogStyle
& wxICON_INFORMATION
)
150 type
= GTK_MESSAGE_INFO
;
151 else if (m_dialogStyle
& wxICON_QUESTION
)
152 type
= GTK_MESSAGE_QUESTION
;
155 // if no style is explicitly specified, detect the suitable icon
156 // ourselves (this can be disabled by using wxICON_NONE)
157 type
= m_dialogStyle
& wxYES
? GTK_MESSAGE_QUESTION
: GTK_MESSAGE_INFO
;
161 #if GTK_CHECK_VERSION(2, 6, 0)
162 bool needsExtMessage
= false;
163 if ( gtk_check_version(2, 6, 0) == NULL
&& !m_extendedMessage
.empty() )
166 needsExtMessage
= true;
168 else // extended message not needed or not supported
171 message
= GetFullMessage();
174 m_widget
= gtk_message_dialog_new(parent
,
179 (const char*)wxGTK_CONV(message
));
181 #if GTK_CHECK_VERSION(2, 6, 0)
182 if ( needsExtMessage
)
184 gtk_message_dialog_format_secondary_text
186 (GtkMessageDialog
*)m_widget
,
188 (const char *)wxGTK_CONV(m_extendedMessage
)
192 #endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2/!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
194 g_object_ref(m_widget
);
196 if (m_caption
!= wxMessageBoxCaptionStr
)
197 gtk_window_set_title(GTK_WINDOW(m_widget
), wxGTK_CONV(m_caption
));
199 GtkDialog
* const dlg
= GTK_DIALOG(m_widget
);
201 if ( m_dialogStyle
& wxSTAY_ON_TOP
)
203 gtk_window_set_keep_above(GTK_WINDOW(m_widget
), TRUE
);
206 // we need to add buttons manually if we use custom labels or always for
207 // Yes/No/Cancel dialog as GTK+ doesn't support it natively and when using
208 // Hildon we add all the buttons manually as it doesn't support too many of
209 // the combinations we may have
210 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
211 static const bool addButtons
= true;
212 #else // !wxUSE_LIBHILDON
213 const bool addButtons
= buttons
== GTK_BUTTONS_NONE
;
214 #endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON
216 if ( m_dialogStyle
& wxYES_NO
) // Yes/No or Yes/No/Cancel dialog
220 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetNoLabel()),
222 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetYesLabel()),
225 if ( m_dialogStyle
& wxCANCEL
)
227 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetCancelLabel()),
228 GTK_RESPONSE_CANCEL
);
232 // it'd probably be harmless to call gtk_dialog_set_default_response()
233 // twice but why do it if we're going to change the default below
235 if ( !(m_dialogStyle
& wxCANCEL_DEFAULT
) )
237 gtk_dialog_set_default_response(dlg
,
238 m_dialogStyle
& wxNO_DEFAULT
243 else if ( addButtons
) // Ok or Ok/Cancel dialog
245 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetOKLabel()), GTK_RESPONSE_OK
);
246 if ( m_dialogStyle
& wxCANCEL
)
248 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetCancelLabel()),
249 GTK_RESPONSE_CANCEL
);
253 if ( m_dialogStyle
& wxCANCEL_DEFAULT
)
255 gtk_dialog_set_default_response(dlg
, GTK_RESPONSE_CANCEL
);
259 int wxMessageDialog::ShowModal()
261 // break the mouse capture as it would interfere with modal dialog (see
262 // wxDialog::ShowModal)
263 wxWindow
* const win
= wxWindow::GetCapture();
265 win
->GTKReleaseMouseAndNotify();
269 GTKCreateMsgDialog();
270 wxCHECK_MSG( m_widget
, wxID_CANCEL
,
271 wxT("failed to create GtkMessageDialog") );
274 // This should be necessary, but otherwise the
275 // parent TLW will disappear..
277 gtk_window_present( GTK_WINDOW(m_parent
->m_widget
) );
279 gint result
= gtk_dialog_run(GTK_DIALOG(m_widget
));
280 gtk_widget_destroy(m_widget
);
281 g_object_unref(m_widget
);
287 wxFAIL_MSG(wxT("unexpected GtkMessageDialog return code"));
290 case GTK_RESPONSE_CANCEL
:
291 case GTK_RESPONSE_DELETE_EVENT
:
292 case GTK_RESPONSE_CLOSE
:
294 case GTK_RESPONSE_OK
:
296 case GTK_RESPONSE_YES
:
298 case GTK_RESPONSE_NO
:
304 #endif // wxUSE_MSGDLG && !defined(__WXGPE__)