removed unnceessary gtk_window_set_transient_for() call already done by gtk_message_d...
[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 <gtk/gtk.h>
29
30 #if wxUSE_LIBHILDON
31 #include <hildon-widgets/hildon-note.h>
32 #endif // wxUSE_LIBHILDON
33
34 IMPLEMENT_CLASS(wxMessageDialog, wxDialog)
35
36 wxMessageDialog::wxMessageDialog(wxWindow *parent,
37 const wxString& message,
38 const wxString& caption,
39 long style,
40 const wxPoint& WXUNUSED(pos))
41 : wxMessageDialogBase(GetParentForModalDialog(parent),
42 message,
43 caption,
44 style)
45 {
46 }
47
48 void wxMessageDialog::GTKCreateMsgDialog()
49 {
50 GtkWindow * const parent = m_parent ? GTK_WINDOW(m_parent->m_widget) : NULL;
51
52 #if wxUSE_LIBHILDON
53 const char *stockIcon;
54 if ( m_dialogStyle & wxICON_ERROR )
55 stockIcon = "qgn_note_gene_syserror";
56 else if ( m_dialogStyle & wxICON_EXCLAMATION )
57 stockIcon = "qgn_note_gene_syswarning";
58 else if ( m_dialogStyle & wxICON_INFORMATION )
59 stockIcon = "qgn_note_info";
60 else if ( m_dialogStyle & wxICON_QUESTION )
61 stockIcon = "qgn_note_confirm";
62 else
63 stockIcon = "";
64
65 // there is no generic note creation function in public API so we have no
66 // choice but to use g_object_new() directly
67 m_widget = (GtkWidget *)g_object_new
68 (
69 HILDON_TYPE_NOTE,
70 "note_type", HILDON_NOTE_CONFIRMATION_BUTTON_TYPE,
71 "description", (const char *)GetFullMessage().utf8_str(),
72 "icon", stockIcon,
73 NULL
74 );
75 #else // !wxUSE_LIBHILDON
76 GtkMessageType type = GTK_MESSAGE_ERROR;
77 GtkButtonsType buttons = GTK_BUTTONS_OK;
78
79 if (m_dialogStyle & wxYES_NO)
80 {
81 if (m_dialogStyle & wxCANCEL)
82 buttons = GTK_BUTTONS_NONE;
83 else
84 buttons = GTK_BUTTONS_YES_NO;
85 }
86
87 if (m_dialogStyle & wxOK)
88 {
89 if (m_dialogStyle & wxCANCEL)
90 buttons = GTK_BUTTONS_OK_CANCEL;
91 else
92 buttons = GTK_BUTTONS_OK;
93 }
94
95 if (m_dialogStyle & wxICON_EXCLAMATION)
96 type = GTK_MESSAGE_WARNING;
97 else if (m_dialogStyle & wxICON_ERROR)
98 type = GTK_MESSAGE_ERROR;
99 else if (m_dialogStyle & wxICON_INFORMATION)
100 type = GTK_MESSAGE_INFO;
101 else if (m_dialogStyle & wxICON_QUESTION)
102 type = GTK_MESSAGE_QUESTION;
103 else
104 {
105 // GTK+ doesn't have a "typeless" msg box, so try to auto detect...
106 type = m_dialogStyle & wxYES ? GTK_MESSAGE_QUESTION : GTK_MESSAGE_INFO;
107 }
108
109 wxString message;
110 #if GTK_CHECK_VERSION(2, 6, 0)
111 bool needsExtMessage = false;
112 if ( gtk_check_version(2, 6, 0) == NULL && !m_extendedMessage.empty() )
113 {
114 message = m_message;
115 needsExtMessage = true;
116 }
117 else // extended message not needed or not supported
118 #endif // GTK+ 2.6+
119 {
120 message = GetFullMessage();
121 }
122
123 m_widget = gtk_message_dialog_new(parent,
124 GTK_DIALOG_MODAL,
125 type,
126 buttons,
127 "%s",
128 (const char*)wxGTK_CONV(message));
129
130 #if GTK_CHECK_VERSION(2, 6, 0)
131 if ( needsExtMessage )
132 {
133 gtk_message_dialog_format_secondary_text
134 (
135 (GtkMessageDialog *)m_widget,
136 "%s",
137 (const char *)wxGTK_CONV(m_extendedMessage)
138 );
139 }
140 #endif // GTK+ 2.6+
141 #endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON
142
143 g_object_ref(m_widget);
144
145 if (m_caption != wxMessageBoxCaptionStr)
146 gtk_window_set_title(GTK_WINDOW(m_widget), wxGTK_CONV(m_caption));
147
148 // we need to add dialogs manually when using Yes/No/Cancel dialog as GTK+
149 // doesn't support it natively and when using Hildon we add all the buttons
150 // manually as it doesn't support too many of the combinations we have
151 GtkDialog * const dlg = GTK_DIALOG(m_widget);
152 if ( m_dialogStyle & wxYES_NO )
153 {
154 if ( m_dialogStyle & wxCANCEL )
155 {
156 gtk_dialog_add_button(dlg, GTK_STOCK_NO, GTK_RESPONSE_NO);
157 gtk_dialog_add_button(dlg, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
158 gtk_dialog_add_button(dlg, GTK_STOCK_YES, GTK_RESPONSE_YES);
159 }
160 #if wxUSE_LIBHILDON
161 else // just Yes/No
162 {
163 gtk_dialog_add_button(dlg, GTK_STOCK_NO, GTK_RESPONSE_NO);
164 gtk_dialog_add_button(dlg, GTK_STOCK_YES, GTK_RESPONSE_YES);
165 }
166 #endif // wxUSE_LIBHILDON
167
168 gtk_dialog_set_default_response(dlg,
169 m_dialogStyle & wxNO_DEFAULT
170 ? GTK_RESPONSE_NO
171 : GTK_RESPONSE_YES);
172 }
173 #if wxUSE_LIBHILDON
174 else // Ok or Ok/Cancel dialog
175 {
176 gtk_dialog_add_button(dlg, GTK_STOCK_OK, GTK_RESPONSE_OK);
177 if ( m_dialogStyle & wxCANCEL )
178 gtk_dialog_add_button(dlg, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
179 }
180 #endif // wxUSE_LIBHILDON
181 }
182
183 int wxMessageDialog::ShowModal()
184 {
185 // break the mouse capture as it would interfere with modal dialog (see
186 // wxDialog::ShowModal)
187 wxWindow * const win = wxWindow::GetCapture();
188 if ( win )
189 win->GTKReleaseMouseAndNotify();
190
191 if ( !m_widget )
192 {
193 GTKCreateMsgDialog();
194 wxCHECK_MSG( m_widget, wxID_CANCEL,
195 _T("failed to create GtkMessageDialog") );
196 }
197
198 // This should be necessary, but otherwise the
199 // parent TLW will disappear..
200 if (m_parent)
201 gtk_window_present( GTK_WINDOW(m_parent->m_widget) );
202
203 gint result = gtk_dialog_run(GTK_DIALOG(m_widget));
204 gtk_widget_destroy(m_widget);
205 g_object_unref(m_widget);
206 m_widget = NULL;
207
208 switch (result)
209 {
210 default:
211 wxFAIL_MSG(_T("unexpected GtkMessageDialog return code"));
212 // fall through
213
214 case GTK_RESPONSE_CANCEL:
215 case GTK_RESPONSE_DELETE_EVENT:
216 case GTK_RESPONSE_CLOSE:
217 return wxID_CANCEL;
218 case GTK_RESPONSE_OK:
219 return wxID_OK;
220 case GTK_RESPONSE_YES:
221 return wxID_YES;
222 case GTK_RESPONSE_NO:
223 return wxID_NO;
224 }
225 }
226
227
228 #endif // wxUSE_MSGDLG && !defined(__WXGPE__)