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