Add wxMessageDialog::GetEffectiveIcon() and use it in all ports.
[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/messagetype.h"
29 #include "wx/gtk/private/mnemonics.h"
30 #include <gtk/gtk.h>
31
32 #if wxUSE_LIBHILDON
33 #include <hildon-widgets/hildon-note.h>
34 #endif // wxUSE_LIBHILDON
35
36 #if wxUSE_LIBHILDON2
37 #include <hildon/hildon.h>
38 #endif // wxUSE_LIBHILDON2
39
40 IMPLEMENT_CLASS(wxMessageDialog, wxDialog)
41
42 wxMessageDialog::wxMessageDialog(wxWindow *parent,
43 const wxString& message,
44 const wxString& caption,
45 long style,
46 const wxPoint& WXUNUSED(pos))
47 : wxMessageDialogWithCustomLabels(GetParentForModalDialog(parent),
48 message,
49 caption,
50 style)
51 {
52 }
53
54 wxString wxMessageDialog::GetDefaultYesLabel() const
55 {
56 return GTK_STOCK_YES;
57 }
58
59 wxString wxMessageDialog::GetDefaultNoLabel() const
60 {
61 return GTK_STOCK_NO;
62 }
63
64 wxString wxMessageDialog::GetDefaultOKLabel() const
65 {
66 return GTK_STOCK_OK;
67 }
68
69 wxString wxMessageDialog::GetDefaultCancelLabel() const
70 {
71 return GTK_STOCK_CANCEL;
72 }
73
74 void wxMessageDialog::DoSetCustomLabel(wxString& var, const ButtonLabel& label)
75 {
76 int stockId = label.GetStockId();
77 if ( stockId == wxID_NONE )
78 {
79 wxMessageDialogWithCustomLabels::DoSetCustomLabel(var, label);
80 var = wxConvertMnemonicsToGTK(var);
81 }
82 else // stock label
83 {
84 var = wxGetStockGtkID(stockId);
85 }
86 }
87
88 void wxMessageDialog::GTKCreateMsgDialog()
89 {
90 GtkWindow * const parent = m_parent ? GTK_WINDOW(m_parent->m_widget) : NULL;
91
92 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
93 const char *stockIcon = "";
94
95 switch ( GetEffectiveIcon() )
96 {
97 case wxICON_ERROR:
98 stockIcon = "qgn_note_gene_syserror";
99 break;
100
101 case wxICON_WARNING:
102 stockIcon = "qgn_note_gene_syswarning";
103 break;
104
105 case wxICON_QUESTION:
106 stockIcon = "qgn_note_confirm";
107 break;
108
109 case wxICON_INFORMATION:
110 stockIcon = "qgn_note_info";
111 break;
112 }
113
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
117 (
118 HILDON_TYPE_NOTE,
119 #if wxUSE_LIBHILDON
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(),
125 "icon", stockIcon,
126 NULL
127 );
128 #else // !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
129 GtkMessageType type = GTK_MESSAGE_ERROR;
130 GtkButtonsType buttons = GTK_BUTTONS_NONE;
131
132 // when using custom labels, we have to add all the buttons ourselves
133 if ( !HasCustomLabels() )
134 {
135 if ( m_dialogStyle & wxYES_NO )
136 {
137 if ( !(m_dialogStyle & wxCANCEL) )
138 buttons = GTK_BUTTONS_YES_NO;
139 //else: no standard GTK_BUTTONS_YES_NO_CANCEL so leave as NONE
140 }
141 else if ( m_dialogStyle & wxOK )
142 {
143 buttons = m_dialogStyle & wxCANCEL ? GTK_BUTTONS_OK_CANCEL
144 : GTK_BUTTONS_OK;
145 }
146 }
147
148 if ( !wxGTKImpl::ConvertMessageTypeFromWX(GetEffectiveIcon(), &type) )
149 {
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;
153 }
154
155 wxString message;
156 #if GTK_CHECK_VERSION(2, 6, 0)
157 bool needsExtMessage = false;
158 if ( gtk_check_version(2, 6, 0) == NULL && !m_extendedMessage.empty() )
159 {
160 message = m_message;
161 needsExtMessage = true;
162 }
163 else // extended message not needed or not supported
164 #endif // GTK+ 2.6+
165 {
166 message = GetFullMessage();
167 }
168
169 m_widget = gtk_message_dialog_new(parent,
170 GTK_DIALOG_MODAL,
171 type,
172 buttons,
173 "%s",
174 (const char*)wxGTK_CONV(message));
175
176 #if GTK_CHECK_VERSION(2, 6, 0)
177 if ( needsExtMessage )
178 {
179 gtk_message_dialog_format_secondary_text
180 (
181 (GtkMessageDialog *)m_widget,
182 "%s",
183 (const char *)wxGTK_CONV(m_extendedMessage)
184 );
185 }
186 #endif // GTK+ 2.6+
187 #endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2/!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
188
189 g_object_ref(m_widget);
190
191 if (m_caption != wxMessageBoxCaptionStr)
192 gtk_window_set_title(GTK_WINDOW(m_widget), wxGTK_CONV(m_caption));
193
194 GtkDialog * const dlg = GTK_DIALOG(m_widget);
195
196 if ( m_dialogStyle & wxSTAY_ON_TOP )
197 {
198 gtk_window_set_keep_above(GTK_WINDOW(m_widget), TRUE);
199 }
200
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
210
211 if ( m_dialogStyle & wxYES_NO ) // Yes/No or Yes/No/Cancel dialog
212 {
213 if ( addButtons )
214 {
215 gtk_dialog_add_button(dlg, wxGTK_CONV(GetNoLabel()),
216 GTK_RESPONSE_NO);
217 gtk_dialog_add_button(dlg, wxGTK_CONV(GetYesLabel()),
218 GTK_RESPONSE_YES);
219
220 if ( m_dialogStyle & wxCANCEL )
221 {
222 gtk_dialog_add_button(dlg, wxGTK_CONV(GetCancelLabel()),
223 GTK_RESPONSE_CANCEL);
224 }
225 }
226
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
229 // anyhow
230 if ( !(m_dialogStyle & wxCANCEL_DEFAULT) )
231 {
232 gtk_dialog_set_default_response(dlg,
233 m_dialogStyle & wxNO_DEFAULT
234 ? GTK_RESPONSE_NO
235 : GTK_RESPONSE_YES);
236 }
237 }
238 else if ( addButtons ) // Ok or Ok/Cancel dialog
239 {
240 gtk_dialog_add_button(dlg, wxGTK_CONV(GetOKLabel()), GTK_RESPONSE_OK);
241 if ( m_dialogStyle & wxCANCEL )
242 {
243 gtk_dialog_add_button(dlg, wxGTK_CONV(GetCancelLabel()),
244 GTK_RESPONSE_CANCEL);
245 }
246 }
247
248 if ( m_dialogStyle & wxCANCEL_DEFAULT )
249 {
250 gtk_dialog_set_default_response(dlg, GTK_RESPONSE_CANCEL);
251 }
252 }
253
254 int wxMessageDialog::ShowModal()
255 {
256 // break the mouse capture as it would interfere with modal dialog (see
257 // wxDialog::ShowModal)
258 wxWindow * const win = wxWindow::GetCapture();
259 if ( win )
260 win->GTKReleaseMouseAndNotify();
261
262 if ( !m_widget )
263 {
264 GTKCreateMsgDialog();
265 wxCHECK_MSG( m_widget, wxID_CANCEL,
266 wxT("failed to create GtkMessageDialog") );
267 }
268
269 // This should be necessary, but otherwise the
270 // parent TLW will disappear..
271 if (m_parent)
272 gtk_window_present( GTK_WINDOW(m_parent->m_widget) );
273
274 gint result = gtk_dialog_run(GTK_DIALOG(m_widget));
275 gtk_widget_destroy(m_widget);
276 g_object_unref(m_widget);
277 m_widget = NULL;
278
279 switch (result)
280 {
281 default:
282 wxFAIL_MSG(wxT("unexpected GtkMessageDialog return code"));
283 // fall through
284
285 case GTK_RESPONSE_CANCEL:
286 case GTK_RESPONSE_DELETE_EVENT:
287 case GTK_RESPONSE_CLOSE:
288 return wxID_CANCEL;
289 case GTK_RESPONSE_OK:
290 return wxID_OK;
291 case GTK_RESPONSE_YES:
292 return wxID_YES;
293 case GTK_RESPONSE_NO:
294 return wxID_NO;
295 }
296 }
297
298
299 #endif // wxUSE_MSGDLG && !defined(__WXGPE__)