]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/msgdlg.cpp
Initial implementation of wxWebProtocolHandler and wxWebFileProtocolHandler for the...
[wxWidgets.git] / src / gtk / msgdlg.cpp
CommitLineData
13a7abf9 1/////////////////////////////////////////////////////////////////////////////
e5b50758 2// Name: src/gtk/msgdlg.cpp
13a7abf9
VS
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
e5b50758 9// Licence: wxWindows licence
13a7abf9
VS
10/////////////////////////////////////////////////////////////////////////////
11
13a7abf9
VS
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
88a7a4e1 16 #pragma hdrstop
13a7abf9
VS
17#endif
18
ff654490 19#if wxUSE_MSGDLG && !defined(__WXGPE__)
13a7abf9 20
e1bf3ad3 21#include "wx/msgdlg.h"
88a7a4e1
WS
22
23#ifndef WX_PRECOMP
24 #include "wx/intl.h"
25#endif
26
13a7abf9 27#include "wx/gtk/private.h"
ce668f29 28#include "wx/gtk/private/messagetype.h"
92763588 29#include "wx/gtk/private/mnemonics.h"
13a7abf9
VS
30#include <gtk/gtk.h>
31
c96d7bec
VZ
32#if wxUSE_LIBHILDON
33 #include <hildon-widgets/hildon-note.h>
34#endif // wxUSE_LIBHILDON
35
426d19f1
JS
36#if wxUSE_LIBHILDON2
37 #include <hildon/hildon.h>
38#endif // wxUSE_LIBHILDON2
39
13a7abf9
VS
40IMPLEMENT_CLASS(wxMessageDialog, wxDialog)
41
42wxMessageDialog::wxMessageDialog(wxWindow *parent,
43 const wxString& message,
44 const wxString& caption,
45 long style,
46 const wxPoint& WXUNUSED(pos))
ede7b017 47 : wxMessageDialogBase
cdc48273
VZ
48 (
49 GetParentForModalDialog(parent, style),
50 message,
51 caption,
52 style
53 )
13a7abf9 54{
2afb9e16 55}
13a7abf9 56
92763588
VZ
57wxString wxMessageDialog::GetDefaultYesLabel() const
58{
59 return GTK_STOCK_YES;
60}
61
62wxString wxMessageDialog::GetDefaultNoLabel() const
63{
64 return GTK_STOCK_NO;
65}
66
67wxString wxMessageDialog::GetDefaultOKLabel() const
68{
69 return GTK_STOCK_OK;
70}
71
72wxString wxMessageDialog::GetDefaultCancelLabel() const
73{
74 return GTK_STOCK_CANCEL;
75}
76
e08931c0 77void wxMessageDialog::DoSetCustomLabel(wxString& var, const ButtonLabel& label)
92763588 78{
e08931c0
VZ
79 int stockId = label.GetStockId();
80 if ( stockId == wxID_NONE )
81 {
ede7b017 82 wxMessageDialogBase::DoSetCustomLabel(var, label);
e08931c0
VZ
83 var = wxConvertMnemonicsToGTK(var);
84 }
85 else // stock label
86 {
87 var = wxGetStockGtkID(stockId);
88 }
92763588
VZ
89}
90
2afb9e16
VZ
91void wxMessageDialog::GTKCreateMsgDialog()
92{
c96d7bec
VZ
93 GtkWindow * const parent = m_parent ? GTK_WINDOW(m_parent->m_widget) : NULL;
94
426d19f1 95#if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
a4578b0c
VZ
96 const char *stockIcon = "";
97
98 switch ( GetEffectiveIcon() )
99 {
100 case wxICON_ERROR:
101 stockIcon = "qgn_note_gene_syserror";
102 break;
103
104 case wxICON_WARNING:
105 stockIcon = "qgn_note_gene_syswarning";
106 break;
107
108 case wxICON_QUESTION:
109 stockIcon = "qgn_note_confirm";
110 break;
111
112 case wxICON_INFORMATION:
113 stockIcon = "qgn_note_info";
114 break;
115 }
c96d7bec
VZ
116
117 // there is no generic note creation function in public API so we have no
118 // choice but to use g_object_new() directly
119 m_widget = (GtkWidget *)g_object_new
120 (
121 HILDON_TYPE_NOTE,
426d19f1 122#if wxUSE_LIBHILDON
c96d7bec 123 "note_type", HILDON_NOTE_CONFIRMATION_BUTTON_TYPE,
426d19f1
JS
124#else // wxUSE_LIBHILDON
125 "note_type", HILDON_NOTE_TYPE_CONFIRMATION_BUTTON,
126#endif // wxUSE_LIBHILDON /wxUSE_LIBHILDON2
c96d7bec
VZ
127 "description", (const char *)GetFullMessage().utf8_str(),
128 "icon", stockIcon,
129 NULL
130 );
426d19f1 131#else // !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
b63b07a8 132 GtkMessageType type = GTK_MESSAGE_ERROR;
92763588 133 GtkButtonsType buttons = GTK_BUTTONS_NONE;
13a7abf9 134
92763588
VZ
135 // when using custom labels, we have to add all the buttons ourselves
136 if ( !HasCustomLabels() )
13a7abf9 137 {
92763588
VZ
138 if ( m_dialogStyle & wxYES_NO )
139 {
140 if ( !(m_dialogStyle & wxCANCEL) )
141 buttons = GTK_BUTTONS_YES_NO;
142 //else: no standard GTK_BUTTONS_YES_NO_CANCEL so leave as NONE
143 }
144 else if ( m_dialogStyle & wxOK )
145 {
146 buttons = m_dialogStyle & wxCANCEL ? GTK_BUTTONS_OK_CANCEL
147 : GTK_BUTTONS_OK;
148 }
13a7abf9 149 }
b63b07a8 150
a4578b0c 151 if ( !wxGTKImpl::ConvertMessageTypeFromWX(GetEffectiveIcon(), &type) )
e0ae1a0a 152 {
7e3204b4
VZ
153 // if no style is explicitly specified, detect the suitable icon
154 // ourselves (this can be disabled by using wxICON_NONE)
2afb9e16 155 type = m_dialogStyle & wxYES ? GTK_MESSAGE_QUESTION : GTK_MESSAGE_INFO;
e0ae1a0a 156 }
13a7abf9 157
2afb9e16 158 wxString message;
ff9bb105 159#if GTK_CHECK_VERSION(2, 6, 0)
2afb9e16 160 bool needsExtMessage = false;
1878d1ec 161 if ( gtk_check_version(2, 6, 0) == NULL && !m_extendedMessage.empty() )
2afb9e16
VZ
162 {
163 message = m_message;
164 needsExtMessage = true;
165 }
166 else // extended message not needed or not supported
c96d7bec 167#endif // GTK+ 2.6+
2afb9e16
VZ
168 {
169 message = GetFullMessage();
170 }
171
c96d7bec 172 m_widget = gtk_message_dialog_new(parent,
b2ce5e1b 173 GTK_DIALOG_MODAL,
2afb9e16
VZ
174 type,
175 buttons,
176 "%s",
177 (const char*)wxGTK_CONV(message));
178
ff9bb105 179#if GTK_CHECK_VERSION(2, 6, 0)
2afb9e16
VZ
180 if ( needsExtMessage )
181 {
182 gtk_message_dialog_format_secondary_text
183 (
184 (GtkMessageDialog *)m_widget,
185 "%s",
186 (const char *)wxGTK_CONV(m_extendedMessage)
187 );
188 }
c96d7bec 189#endif // GTK+ 2.6+
426d19f1 190#endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2/!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
2afb9e16 191
9ff9d30c
PC
192 g_object_ref(m_widget);
193
13a7abf9 194 if (m_caption != wxMessageBoxCaptionStr)
b2ce5e1b 195 gtk_window_set_title(GTK_WINDOW(m_widget), wxGTK_CONV(m_caption));
13a7abf9 196
c96d7bec 197 GtkDialog * const dlg = GTK_DIALOG(m_widget);
92763588 198
4566dcbe
VZ
199 if ( m_dialogStyle & wxSTAY_ON_TOP )
200 {
201 gtk_window_set_keep_above(GTK_WINDOW(m_widget), TRUE);
202 }
203
92763588
VZ
204 // we need to add buttons manually if we use custom labels or always for
205 // Yes/No/Cancel dialog as GTK+ doesn't support it natively and when using
206 // Hildon we add all the buttons manually as it doesn't support too many of
207 // the combinations we may have
426d19f1 208#if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
92763588
VZ
209 static const bool addButtons = true;
210#else // !wxUSE_LIBHILDON
211 const bool addButtons = buttons == GTK_BUTTONS_NONE;
212#endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON
213
214 if ( m_dialogStyle & wxYES_NO ) // Yes/No or Yes/No/Cancel dialog
215 {
216 if ( addButtons )
c96d7bec 217 {
5d3056f1
VZ
218 // Add the buttons in the correct order which is, according to
219 // http://library.gnome.org/devel/hig-book/stable/windows-alert.html.en
220 // the following one:
221 //
222 // [Help] [Alternative] [Cancel] [Affirmative]
223
150c8eb9
VZ
224 gtk_dialog_add_button(dlg, wxGTK_CONV(GetNoLabel()),
225 GTK_RESPONSE_NO);
92763588
VZ
226
227 if ( m_dialogStyle & wxCANCEL )
228 {
150c8eb9 229 gtk_dialog_add_button(dlg, wxGTK_CONV(GetCancelLabel()),
92763588
VZ
230 GTK_RESPONSE_CANCEL);
231 }
5d3056f1
VZ
232
233 gtk_dialog_add_button(dlg, wxGTK_CONV(GetYesLabel()),
234 GTK_RESPONSE_YES);
c96d7bec 235 }
c96d7bec 236
24689293
VZ
237 // it'd probably be harmless to call gtk_dialog_set_default_response()
238 // twice but why do it if we're going to change the default below
239 // anyhow
240 if ( !(m_dialogStyle & wxCANCEL_DEFAULT) )
241 {
242 gtk_dialog_set_default_response(dlg,
243 m_dialogStyle & wxNO_DEFAULT
244 ? GTK_RESPONSE_NO
245 : GTK_RESPONSE_YES);
246 }
c96d7bec 247 }
92763588 248 else if ( addButtons ) // Ok or Ok/Cancel dialog
c96d7bec 249 {
150c8eb9 250 gtk_dialog_add_button(dlg, wxGTK_CONV(GetOKLabel()), GTK_RESPONSE_OK);
c96d7bec 251 if ( m_dialogStyle & wxCANCEL )
150c8eb9
VZ
252 {
253 gtk_dialog_add_button(dlg, wxGTK_CONV(GetCancelLabel()),
254 GTK_RESPONSE_CANCEL);
255 }
13a7abf9 256 }
24689293
VZ
257
258 if ( m_dialogStyle & wxCANCEL_DEFAULT )
259 {
260 gtk_dialog_set_default_response(dlg, GTK_RESPONSE_CANCEL);
261 }
b2ce5e1b 262}
e5b50758 263
b2ce5e1b
RD
264int wxMessageDialog::ShowModal()
265{
7738af59
VZ
266 // break the mouse capture as it would interfere with modal dialog (see
267 // wxDialog::ShowModal)
268 wxWindow * const win = wxWindow::GetCapture();
269 if ( win )
270 win->GTKReleaseMouseAndNotify();
271
2afb9e16
VZ
272 if ( !m_widget )
273 {
274 GTKCreateMsgDialog();
275 wxCHECK_MSG( m_widget, wxID_CANCEL,
9a83f860 276 wxT("failed to create GtkMessageDialog") );
2afb9e16
VZ
277 }
278
3b439e60
RR
279 // This should be necessary, but otherwise the
280 // parent TLW will disappear..
281 if (m_parent)
282 gtk_window_present( GTK_WINDOW(m_parent->m_widget) );
e5b50758 283
b2ce5e1b
RD
284 gint result = gtk_dialog_run(GTK_DIALOG(m_widget));
285 gtk_widget_destroy(m_widget);
9ff9d30c 286 g_object_unref(m_widget);
b2ce5e1b 287 m_widget = NULL;
13a7abf9
VS
288
289 switch (result)
290 {
291 default:
9a83f860 292 wxFAIL_MSG(wxT("unexpected GtkMessageDialog return code"));
13a7abf9
VS
293 // fall through
294
b63b07a8 295 case GTK_RESPONSE_CANCEL:
0e4a7045
VS
296 case GTK_RESPONSE_DELETE_EVENT:
297 case GTK_RESPONSE_CLOSE:
13a7abf9
VS
298 return wxID_CANCEL;
299 case GTK_RESPONSE_OK:
300 return wxID_OK;
301 case GTK_RESPONSE_YES:
302 return wxID_YES;
303 case GTK_RESPONSE_NO:
304 return wxID_NO;
305 }
306}
307
b2ce5e1b 308
ff654490 309#endif // wxUSE_MSGDLG && !defined(__WXGPE__)