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