added support for custom labels in wxMessageBox for wxGTK too
[wxWidgets.git] / src / gtk / msgdlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/msgdlg.cpp
3 // Purpose: wxMessageDialog for GTK+2
4 // Author: Vaclav Slavik
5 // Modified by:
6 // Created: 2003/02/28
7 // RCS-ID: $Id$
8 // Copyright: (c) Vaclav Slavik, 2003
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_MSGDLG && !defined(__WXGPE__)
20
21 #include "wx/msgdlg.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/intl.h"
25 #endif
26
27 #include "wx/gtk/private.h"
28 #include "wx/gtk/private/mnemonics.h"
29 #include <gtk/gtk.h>
30
31 #if wxUSE_LIBHILDON
32 #include <hildon-widgets/hildon-note.h>
33 #endif // wxUSE_LIBHILDON
34
35 IMPLEMENT_CLASS(wxMessageDialog, wxDialog)
36
37 wxMessageDialog::wxMessageDialog(wxWindow *parent,
38 const wxString& message,
39 const wxString& caption,
40 long style,
41 const wxPoint& WXUNUSED(pos))
42 : wxMessageDialogWithCustomLabels(GetParentForModalDialog(parent),
43 message,
44 caption,
45 style)
46 {
47 }
48
49 wxString wxMessageDialog::GetDefaultYesLabel() const
50 {
51 return GTK_STOCK_YES;
52 }
53
54 wxString wxMessageDialog::GetDefaultNoLabel() const
55 {
56 return GTK_STOCK_NO;
57 }
58
59 wxString wxMessageDialog::GetDefaultOKLabel() const
60 {
61 return GTK_STOCK_OK;
62 }
63
64 wxString wxMessageDialog::GetDefaultCancelLabel() const
65 {
66 return GTK_STOCK_CANCEL;
67 }
68
69 void wxMessageDialog::DoSetCustomLabel(wxString& var, const wxString& value)
70 {
71 var = wxConvertMnemonicsToGTK(value);
72 }
73
74 void wxMessageDialog::GTKCreateMsgDialog()
75 {
76 GtkWindow * const parent = m_parent ? GTK_WINDOW(m_parent->m_widget) : NULL;
77
78 #if wxUSE_LIBHILDON
79 const char *stockIcon;
80 if ( m_dialogStyle & wxICON_ERROR )
81 stockIcon = "qgn_note_gene_syserror";
82 else if ( m_dialogStyle & wxICON_EXCLAMATION )
83 stockIcon = "qgn_note_gene_syswarning";
84 else if ( m_dialogStyle & wxICON_INFORMATION )
85 stockIcon = "qgn_note_info";
86 else if ( m_dialogStyle & wxICON_QUESTION )
87 stockIcon = "qgn_note_confirm";
88 else
89 stockIcon = "";
90
91 // there is no generic note creation function in public API so we have no
92 // choice but to use g_object_new() directly
93 m_widget = (GtkWidget *)g_object_new
94 (
95 HILDON_TYPE_NOTE,
96 "note_type", HILDON_NOTE_CONFIRMATION_BUTTON_TYPE,
97 "description", (const char *)GetFullMessage().utf8_str(),
98 "icon", stockIcon,
99 NULL
100 );
101 #else // !wxUSE_LIBHILDON
102 GtkMessageType type = GTK_MESSAGE_ERROR;
103 GtkButtonsType buttons = GTK_BUTTONS_NONE;
104
105 // when using custom labels, we have to add all the buttons ourselves
106 if ( !HasCustomLabels() )
107 {
108 if ( m_dialogStyle & wxYES_NO )
109 {
110 if ( !(m_dialogStyle & wxCANCEL) )
111 buttons = GTK_BUTTONS_YES_NO;
112 //else: no standard GTK_BUTTONS_YES_NO_CANCEL so leave as NONE
113 }
114 else if ( m_dialogStyle & wxOK )
115 {
116 buttons = m_dialogStyle & wxCANCEL ? GTK_BUTTONS_OK_CANCEL
117 : GTK_BUTTONS_OK;
118 }
119 }
120
121 if (m_dialogStyle & wxICON_EXCLAMATION)
122 type = GTK_MESSAGE_WARNING;
123 else if (m_dialogStyle & wxICON_ERROR)
124 type = GTK_MESSAGE_ERROR;
125 else if (m_dialogStyle & wxICON_INFORMATION)
126 type = GTK_MESSAGE_INFO;
127 else if (m_dialogStyle & wxICON_QUESTION)
128 type = GTK_MESSAGE_QUESTION;
129 else
130 {
131 // GTK+ doesn't have a "typeless" msg box, so try to auto detect...
132 type = m_dialogStyle & wxYES ? GTK_MESSAGE_QUESTION : GTK_MESSAGE_INFO;
133 }
134
135 wxString message;
136 #if GTK_CHECK_VERSION(2, 6, 0)
137 bool needsExtMessage = false;
138 if ( gtk_check_version(2, 6, 0) == NULL && !m_extendedMessage.empty() )
139 {
140 message = m_message;
141 needsExtMessage = true;
142 }
143 else // extended message not needed or not supported
144 #endif // GTK+ 2.6+
145 {
146 message = GetFullMessage();
147 }
148
149 m_widget = gtk_message_dialog_new(parent,
150 GTK_DIALOG_MODAL,
151 type,
152 buttons,
153 "%s",
154 (const char*)wxGTK_CONV(message));
155
156 #if GTK_CHECK_VERSION(2, 6, 0)
157 if ( needsExtMessage )
158 {
159 gtk_message_dialog_format_secondary_text
160 (
161 (GtkMessageDialog *)m_widget,
162 "%s",
163 (const char *)wxGTK_CONV(m_extendedMessage)
164 );
165 }
166 #endif // GTK+ 2.6+
167 #endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON
168
169 g_object_ref(m_widget);
170
171 if (m_caption != wxMessageBoxCaptionStr)
172 gtk_window_set_title(GTK_WINDOW(m_widget), wxGTK_CONV(m_caption));
173
174 GtkDialog * const dlg = GTK_DIALOG(m_widget);
175
176 // we need to add buttons manually if we use custom labels or always for
177 // Yes/No/Cancel dialog as GTK+ doesn't support it natively and when using
178 // Hildon we add all the buttons manually as it doesn't support too many of
179 // the combinations we may have
180 #if wxUSE_LIBHILDON
181 static const bool addButtons = true;
182 #else // !wxUSE_LIBHILDON
183 const bool addButtons = buttons == GTK_BUTTONS_NONE;
184 #endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON
185
186 if ( m_dialogStyle & wxYES_NO ) // Yes/No or Yes/No/Cancel dialog
187 {
188 if ( addButtons )
189 {
190 gtk_dialog_add_button(dlg, GetNoLabel(), GTK_RESPONSE_NO);
191 gtk_dialog_add_button(dlg, GetYesLabel(), GTK_RESPONSE_YES);
192
193 if ( m_dialogStyle & wxCANCEL )
194 {
195 gtk_dialog_add_button(dlg, GetCancelLabel(),
196 GTK_RESPONSE_CANCEL);
197 }
198 }
199
200 gtk_dialog_set_default_response(dlg,
201 m_dialogStyle & wxNO_DEFAULT
202 ? GTK_RESPONSE_NO
203 : GTK_RESPONSE_YES);
204 }
205 else if ( addButtons ) // Ok or Ok/Cancel dialog
206 {
207 gtk_dialog_add_button(dlg, GetOKLabel(), GTK_RESPONSE_OK);
208 if ( m_dialogStyle & wxCANCEL )
209 gtk_dialog_add_button(dlg, GetCancelLabel(), GTK_RESPONSE_CANCEL);
210 }
211 }
212
213 int wxMessageDialog::ShowModal()
214 {
215 // break the mouse capture as it would interfere with modal dialog (see
216 // wxDialog::ShowModal)
217 wxWindow * const win = wxWindow::GetCapture();
218 if ( win )
219 win->GTKReleaseMouseAndNotify();
220
221 if ( !m_widget )
222 {
223 GTKCreateMsgDialog();
224 wxCHECK_MSG( m_widget, wxID_CANCEL,
225 _T("failed to create GtkMessageDialog") );
226 }
227
228 // This should be necessary, but otherwise the
229 // parent TLW will disappear..
230 if (m_parent)
231 gtk_window_present( GTK_WINDOW(m_parent->m_widget) );
232
233 gint result = gtk_dialog_run(GTK_DIALOG(m_widget));
234 gtk_widget_destroy(m_widget);
235 g_object_unref(m_widget);
236 m_widget = NULL;
237
238 switch (result)
239 {
240 default:
241 wxFAIL_MSG(_T("unexpected GtkMessageDialog return code"));
242 // fall through
243
244 case GTK_RESPONSE_CANCEL:
245 case GTK_RESPONSE_DELETE_EVENT:
246 case GTK_RESPONSE_CLOSE:
247 return wxID_CANCEL;
248 case GTK_RESPONSE_OK:
249 return wxID_OK;
250 case GTK_RESPONSE_YES:
251 return wxID_YES;
252 case GTK_RESPONSE_NO:
253 return wxID_NO;
254 }
255 }
256
257
258 #endif // wxUSE_MSGDLG && !defined(__WXGPE__)